[英]Group mysql result into an php array
I have an result fetched by mysql join like:我有一个由 mysql join 获取的结果,如:
Array
(
[0] => Array
(
[job_card_id] => 1
[repair_order_id] => 1
[customer_request] => test customer request
[qty] => 5
[registration_no] => DL 17U 7113
)
[1] => Array
(
[job_card_id] => 1
[repair_order_id] => 2
[customer_request] => test customer request1
[qty] => 5
[registration_no] => DL 17U 7113
)
[2] => Array
(
[job_card_id] => 2
[repair_order_id] => 4
[customer_request] => test customer request
[qty] => 5
[registration_no] => DL 17U 6111
)
[3] => Array
(
[job_card_id] => 2
[repair_order_id] => 5
[customer_request] => test customer request1
[qty] => 5
[registration_no] => DL 17U 6111
)
)
And I want to group it based on job_card_id
and registration_no
like:我想根据job_card_id
和registration_no
对其进行job_card_id
,例如:
Array(
[0]=>Array
(
[job_card_id] => 1
[registration_no] => DL 17U 7113
[repair_order]=>Array
(
[0]=>Array
(
[repair_order_id] => 1
[customer_request] => test customer request
[qty] => 5
)
[1]=>Array
(
[repair_order_id] => 2
[customer_request] => test customer request1
[qty] => 5
)
)
)
[1]=>Array
(
[job_card_id] => 2
[registration_no] => DL 17U 6111
[repair_order]=>Array
(
[0]=>Array
(
[repair_order_id] => 4
[customer_request] => test customer request
[qty] => 5
)
[1]=>Array
(
[repair_order_id] => 5
[customer_request] => test customer request1
[qty] => 5
)
)
)
)
How I can produce this type of result,Please help, Thanks in advance.我如何产生这种类型的结果,请帮忙,提前致谢。
You can use bu iterate over the your array and create new array:您可以使用 bu 迭代您的数组并创建新数组:
$newArr = [];
foreach($arr as $value)
{
$key = $value['job_card_id'].$value['registration_no'];
if (!isset($newArr[$key]))
{
$newArr[$key] = [
'job_card_id' => $value['job_card_id'],
'registration_no' => $value['registration_no'],
'repair_order' => [],
];
}
$newArr[$key]['repair_order'][] = [
'repair_order_id' => $value['repair_order_id'],
'customer_request'=> $value['customer_request'],
'qty' => $value['qty'],
];
}
$newArr = array_values($newArr);
$arr
is your array $arr
是你的数组
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.