Php 重复id或其他reord上的数组中的数组
我正在用PHP为一个移动应用程序编写Web服务,必须在订单详细信息服务的get查询中输入一些数据。 当我编码成json时,我的查询得到以下响应Php 重复id或其他reord上的数组中的数组,php,mysql,json,multidimensional-array,associative-array,Php,Mysql,Json,Multidimensional Array,Associative Array,我正在用PHP为一个移动应用程序编写Web服务,必须在订单详细信息服务的get查询中输入一些数据。 当我编码成json时,我的查询得到以下响应 { "status": 101, "message": "Success", "result": [ { "order_id": "38", "order_product_id": "49", "product_id": "377", "product_name": "Pineapple
{
"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);
先谢谢你
Priyanka我获取了您的JSON示例,并编写了以下代码以获得预期的输出。希望能有帮助
$data = '[
{
"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"
}
]';
$data = json_decode($data, true);
$final = [];
foreach ($data as $_d) {
$index = $_d['order_id'] . '.' . $_d['order_product_id'] . '.' . $_d['product_id'];
if (isset($final[$index])) {
$final[$index]['result2'][] = [
'option_name' => $_d['option_name'],
'option_value' => $_d['option_value']
];
} else {
$final[$index] = [
'order_id' => $_d['order_id'],
'order_product_id' => $_d['order_product_id'],
'product_id' => $_d['product_id'],
'product_name' => $_d['product_name'],
'model' => $_d['model'],
'quantity' => $_d['quantity']
];
$final[$index]['result2'][] = [
'option_name' => $_d['option_name'],
'option_value' => $_d['option_value']
];
}
}
print_r(array_values($final));
输出:
Array
(
[0] => Array
(
[order_id] => 38
[order_product_id] => 49
[product_id] => 377
[product_name] => Pineapple Cake 2 Kg Eggless + Free Surprise Gift
[model] => Cake B22
[quantity] => 1
[result2] => Array
(
[0] => Array
(
[option_name] => Delivery Time
[option_value] => 4:00am to 6:00am
)
[1] => Array
(
[option_name] => Message For Cake
[option_value] => Second order first product
)
)
)
[1] => Array
(
[order_id] => 38
[order_product_id] => 50
[product_id] => 339
[product_name] => Pineapple Cake 1 Kg + Free Surprise Gift
[model] => Cake A7
[quantity] => 1
[result2] => Array
(
[0] => Array
(
[option_name] => Make it Eggless
[option_value] => No
)
[1] => Array
(
[option_name] => Delivery Time
[option_value] => 4:00am to 6:00am
)
[2] => Array
(
[option_name] => Message For Cake
[option_value] => second order 2nd cake
)
)
)
)
Array
(
[0] => Array
(
[order_id] => 38
[order_product_id] => 49
[product_id] => 377
[product_name] => Pineapple Cake 2 Kg Eggless + Free Surprise Gift
[model] => Cake B22
[quantity] => 1
[result2] => Array
(
[0] => Array
(
[option_name] => Delivery Time
[option_value] => 4:00am to 6:00am
)
[1] => Array
(
[option_name] => Message For Cake
[option_value] => Second order first product
)
)
)
[1] => Array
(
[order_id] => 38
[order_product_id] => 50
[product_id] => 339
[product_name] => Pineapple Cake 1 Kg + Free Surprise Gift
[model] => Cake A7
[quantity] => 1
[result2] => Array
(
[0] => Array
(
[option_name] => Make it Eggless
[option_value] => No
)
[1] => Array
(
[option_name] => Delivery Time
[option_value] => 4:00am to 6:00am
)
[2] => Array
(
[option_name] => Message For Cake
[option_value] => second order 2nd cake
)
)
)
)