创建数据 json php
create data 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 有点困惑如何实现我想要的响应
从 while 循环中删除初始化
$detailItem=[];
根据要求更新了代码:
试试这个代码:
我希望它有用。
$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;
我有点困惑如何创建这样的响应数据。
[
{
"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 有点困惑如何实现我想要的响应
从 while 循环中删除初始化
$detailItem=[];
根据要求更新了代码:
试试这个代码:
我希望它有用。
$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;