【问题标题】:create data json php创建数据 json php
【发布时间】:2017-04-19 02:39:22
【问题描述】:

我有点困惑如何创建这样的响应数据。

  [
  {
    "profile": "Profile 1",
    "kumpulan_spot": [
      {
         "urutan": "1",
        "id": "0415AA9E2D",
        "checkpoint": "3. OFFICE DEPAN LT. 2",
        "lokasi": "WARU",
        "waktu": "none",
        "status": "none",
        "kondisi": "none",
        "foto": "none"
      },
      {
         "urutan": "2",
        "id": "0415AAA007",
        "checkpoint": "4. OFFICE KRIAN LT.3 - R. MIS",
        "lokasi": "KRIAN",
        "waktu": "none",
        "status": "none",
        "kondisi": "none",
        "foto": "none"
      }
    ]
  },
  {
    "profile": "Profile 2",
    "kumpulan_spot": [
      {
        "urutan": "3",
        "id": "0415AAA01B",
        "checkpoint": "2. OFFICE DEPAN LT.1 - R. MARKETING",
        "lokasi": "KRIAN",
        "waktu": "none",
        "status": "none",
        "kondisi": "none",
        "foto": "none"
      }
    ]
  }
]

但到目前为止我一直在尝试什么

$myparams['nik'] = $_POST['nik']; 

$procedure_params = array( 
array(&$myparams['nik']  , SQLSRV_PARAM_IN)

);
$sql = "EXEC SP_GETJADWAL @nik=?";
$stmt = sqlsrv_prepare($conn, $sql,$procedure_params);

 if( !$stmt ) {
die( print_r( sqlsrv_errors(), true));
}
$return_hasil=[];
$myArray = []; 
if(sqlsrv_execute($stmt)){ 
    do {
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        $arrItem=[];         
        $detailItem=[];
        // $i=0;

        //  while($i==0){
            $arrItem["Profile"]="Profile ".$row['Profile']; 
            // $i=1;
            //  }
            $detailCheckPoint=[];   
            $detailCheckPoint["urutan"]=$row['Urutan'];         
            $detailCheckPoint["id"]=$row['check_point_id'];
            $detailCheckPoint["checkpoint"]=$row['check_point_name'];
            $detailCheckPoint["lokasi"]=$row['check_point_loc'];
            $detailCheckPoint["waktu"]="none";
            $detailCheckPoint["status"]="none";
            $detailCheckPoint["kondisi"]="none";
            $detailCheckPoint["foto"]="none";
            $detailItem[]=$detailCheckPoint;    
            $arrItem["ListCheckPoint"]=$detailItem;
            $return_hasil[]=$arrItem;


    }


    } while ( sqlsrv_next_result($stmt) );
     echo json_encode($return_hasil);
}else{
  die( print_r( sqlsrv_errors(), true));
}
 }else{
     echo 'Connection Failure<br/>';
 }

得到这样的结果

[
  {
    "Profile": "Profile 1",
    "ListCheckPoint": [
      {
        "urutan": "1",
        "id": "0415AA9E2D",
        "checkpoint": "3. OFFICE DEPAN LT. 2",
        "lokasi": "WARU",
        "waktu": "none",
        "status": "none",
        "kondisi": "none",
        "foto": "none"
      }
    ]
  },
  {
    "Profile": "Profile 1",
    "ListCheckPoint": [
      {
        "urutan": "2",
        "id": "0415AAA007",
        "checkpoint": "4. OFFICE KRIAN LT.3 - R. MIS",
        "lokasi": "KRIAN",
        "waktu": "none",
        "status": "none",
        "kondisi": "none",
        "foto": "none"
      }
    ]
  },
  {
    "Profile": "Profile 1",
    "ListCheckPoint": [
      {
        "urutan": "3",
        "id": "0415AAA01B",
        "checkpoint": "2. OFFICE DEPAN LT.1 - R. MARKETING",
        "lokasi": "KRIAN",
        "waktu": "none",
        "status": "none",
        "kondisi": "none",
        "foto": "none"
      }
    ]
  }
]

我是创建数据 json 的新手,有点困惑如何实现我想要的响应

【问题讨论】:

  • 有什么问题?格式看起来正确。
  • 您想在“kumpulan_spot”中插入多个数组。在什么条件下您决定将元素放入“kumpulan_spot”?
  • @TouheedKhan 在条件 $arrItem["Profile"]="Profile ".$row['Profile'];先生,所以个人资料可以有很多 kumpulan_spot

标签: php json sqlsrv


【解决方案1】:

根据要求更新代码:

试试这个代码:

我希望它确实有效。

$arrItem["Profile"] ="Profile ".$row['Profile']; 
$detailCheckPoint=[];   
$detailCheckPoint["urutan"]=$row['Urutan'];         
$detailCheckPoint["id"]=$row['check_point_id'];
$detailCheckPoint["checkpoint"]=$row['check_point_name'];
$detailCheckPoint["lokasi"]=$row['check_point_loc'];
$detailCheckPoint["waktu"]="none";
$detailCheckPoint["status"]="none";
$detailCheckPoint["kondisi"]="none";
$detailCheckPoint["foto"]="none";
foreach ($return_hasil as $key1 => $value) {
    if($value["Profile"] === $arrItem["Profile"]) {
        var_dump($detailCheckPoint);
        $return_hasil[$key1]["ListCheckPoint"][] = $detailCheckPoint;
        continue 2;
    }
}
$arrItem['ListCheckPoint'] = array($detailCheckPoint);
$return_hasil[]=$arrItem;

【讨论】:

  • 它的工作谢谢你,但有 1 个数据无法显示到我的 json 结构@Touheed Khan
  • @anasrachmadi 什么数据?请解释一下,以便我可以纠正问题。
  • 所以在我的数据中,配置文件 1 包含 json 数组 [1,2,3],配置文件 2 包含 [1,2]。使用您的代码仅显示配置文件 1 [2,3] 和配置文件 2 [2]。 @Touheed Khan
【解决方案2】:

从while循环中移除初始化

 $detailItem=[]; 

【讨论】:

  • 仍然无法正常工作,缺少配置文件 1,只显示配置文件 2 和我的所有元素
  • 从 while 中删除 detailCheckPoint=[]
  • 仍然无法正常工作,它看起来像获取最后一行数据表,因此配置文件 1 不显示 @Nishant Nair
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
  • 2017-08-20
  • 1970-01-01
  • 2013-11-29
  • 1970-01-01
相关资源
最近更新 更多