2017-04-19 97 views
1

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

[ 
    { 
    "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和点点即时通讯新困惑如何实现响应我想要

+0

有什么问题?格式看起来正确。 –

+0

你想在“kumpulan_spot”中插入多个数组。你决定在“kumpulan_spot”中放置元素的条件是什么? –

+0

@TouheedKhan条件$ arrItem [“Profile”] =“Profile”。$ row ['Profile'];先生所以配置文件可以有很多kumpulan_spot –

回答

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; 
+0

其工作谢谢,但有1个数据无法显示给我的json结构@Touheed Khan –

+0

@anasrachmadi有什么数据?请解释,以便我可以纠正问题。 –

+0

所以在我的数据中有配置文件1包含json数组[1,2,3],配置文件2包含[1,2]。与您的代码只显示配置文件1 [2,3]和配置文件2 [2]。 @Touheed Khan –

0

删除while循环的初始化

$detailItem=[]; 
+0

仍然不工作,配置文件1丢失,只是显示配置文件2与我的所有元素 –

+0

删除'detailCheckPoint = []'而 –

+0

仍然无法正常工作,它看起来像它的获取最后一行数据表如此profile 1不显示@Nishant Nair –