将mysql结果分组到一个php数组中

时间:2018-08-20 12:04:31

标签: php mysql arrays

我有一个像mysql join那样获取的结果:

job_card_id

我想基于registration_noArray( [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 ) ) ) ) 将其分组,例如:

WeatherData.toString()

我如何产生这种结果,请帮忙,谢谢。

1 个答案:

答案 0 :(得分:0)

您可以对数组使用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是您的数组