【发布时间】:2017-06-14 18:11:30
【问题描述】:
我正在用 PHP 为移动应用程序编写 Web 服务,并且必须将一些数据放入获取订单详细信息服务的查询中。 当我编码成 json 时,我的查询得到以下响应。
{
"status": 101,
"message": "Success",
"result": [
{
"order_id": "38",
"order_product_id": "49",
"product_id": "377",
"product_name": "Pineapple Cake 2 Kg Eggless + Free Surprise Gift",
"option_name": "Delivery Time",
"option_value": "4:00am to 6:00am",
"model": "Cake B22",
"quantity": "1"
},
{
"order_id": "38",
"order_product_id": "49",
"product_id": "377",
"product_name": "Pineapple Cake 2 Kg Eggless + Free Surprise Gift",
"option_name": "Message For Cake",
"option_value": "Second order first product",
"model": "Cake B22",
"quantity": "1"
},
{
"order_id": "38",
"order_product_id": "50",
"product_id": "339",
"product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
"option_name": "Make it Eggless",
"option_value": "No",
"model": "Cake A7",
"quantity": "1"
},
{
"order_id": "38",
"order_product_id": "50",
"product_id": "339",
"product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
"option_name": "Delivery Time",
"option_value": "4:00am to 6:00am",
"model": "Cake A7",
"quantity": "1"
},
{
"order_id": "38",
"order_product_id": "50",
"product_id": "339",
"product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
"option_name": "Message For Cake",
"option_value": "second order 2nd cake",
"model": "Cake A7",
"quantity": "1"
}
]
}
我通过 sql 查询得到的结果
$strSQL = "SEletc * ........";
$objQuery = mysql_query($strSQL);
$intNumField = mysql_num_fields($objQuery);
$resultArray = array();
$data_ok = false;
while($obResult = mysql_fetch_array($objQuery))
{
$arrCol = array();
$data_ok = false;
for($i=0;$i<$intNumField;$i++)
{
$arrCol[mysql_field_name($objQuery,$i)] = $obResult[$i];
}
array_push($resultArray,$arrCol);
$data_ok = true;
}
if($data_ok) {
$response["status"] = 101;
$response["message"] = "Success";
$response["result"] = $resultArray;
echo json_encode($response);
}
else
{
$response["status"] = 100;
$response["message"] = "No category exist in database";
echo json_encode($response);
}
mysql_close($objConnect);
我想要的是这样的
{
"status": 101,
"message": "Success",
"result": [
{
"order_id": "38",
"order_product_id": "49",
"product_id": "377",
"product_name": "Pineapple Cake 2 Kg Eggless + Free Surprise Gift",
"result2": [
{
"option_name": "Delivery Time",
"option_value": "4:00am to 6:00am",
},
{
"option_name": "Message For Cake",
"option_value": "Second order first product",
}
],
"model": "Cake B22",
"quantity": "1"
},
{
"order_id": "38",
"order_product_id": "50",
"product_id": "339",
"product_name": "Pineapple Cake 1 Kg + Free Surprise Gift",
"result2": [
{
"option_name": "Make it Eggless",
"option_value": "No",
},
{
"option_name": "Delivery Time",
"option_value": "4:00am to 6:00am",
},
{
"option_name": "Message For Cake",
"option_value": "Second order first product",
}
],
"model": "Cake A7",
"quantity": "1"
}
]
}
提前谢谢你 普里扬卡
【问题讨论】:
标签: php mysql json multidimensional-array associative-array