组的MySQL结果为PHP数组

问题描述 投票:-4回答:1

我有由MySQL获取的结果加入这样的:

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
        )

)

我想它的基础上job_card_idregistration_no喜欢组:

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
                        )
                    )
     )
)

我怎么能生产出这种类型的搜索结果,请帮帮忙,先谢谢了。

php mysql arrays
1个回答
0
投票

您可以通过反复调整你的阵列上使用,并创建新的数组:

$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是你的数组

© www.soinside.com 2019 - 2024. All rights reserved.