在多数组上使用array_chunk

时间:2019-01-31 14:32:58

标签: php arrays

我有一个多维数组,现在我确定如何在我的数组键请求上使用数组块,同时将信息保存到新数组中。我想每2个数组拆分一个数组。我尝试在循环内使用array_chunk,但没有运气。

这是我的阵列。

[0] => Array
    (
        [first_name] => Richard
        [patient_first_name] => Donna
        [trip_date] => 2018-08-24
        [request] => Array
            (
                [0] => stdClass Object
                (
                    [id] => 46
                    [client_id] => 9873
                    [city] => COOLIDGE
                    [state] => AZ
                    [zip] => 85228
                )

                [1] => stdClass Object
                (
                    [id] => 49
                    [client_id] => 14965
                    [city] => CHANDLER
                    [state] => AZ
                    [zip] => 85226
                )

                [2] => stdClass Object
                (
                    [id] => 55
                    [client_id] => 10120
                    [city] => PHX
                    [state] => AZ
                    [zip] => 85008
                )

                [3] => stdClass Object
                (
                    [id] => 59
                    [client_id] => 11229
                    [city] => BUCKEYE
                    [state] => AZ
                    [zip] => 85326
                )

                [4] => stdClass Object
                (
                    [id] => 69
                    [client_id] => 13769
                    [city] => PHOENIX
                    [state] => AZ
                    [zip] => 85035
                )

                [5] => stdClass Object
                (
                    [id] => 175
                    [client_id] => 16437
                    [city] => Phx
                    [state] => Az
                    [zip] => 85029
                )

                [6] => stdClass Object
                (
                    [id] => 195
                    [client_id] => 16457
                    [city] => Apache Junction
                    [state] => Az
                    [zip] => 85120
                )

                [7] => stdClass Object
                (
                    [id] => 197
                    [client_id] => 16459
                    [city] => Mesa
                    [state] => Az
                    [zip] => 85204
                )

            )

        )

这是我想要的数组。

 [0] => Array
            (
                [first_name] => Richard
                [patient_first_name] => Donna
                [trip_date] => 2018-08-24
                [request] => Array
                    (
                        [0] => stdClass Object
                        (
                            [id] => 46
                            [client_id] => 9873
                            [city] => COOLIDGE
                            [state] => AZ
                            [zip] => 85228
                        )

                        [1] => stdClass Object
                        (
                            [id] => 49
                            [client_id] => 14965
                            [city] => CHANDLER
                            [state] => AZ
                            [zip] => 85226
                        )
)

        [1] => Array
            (
                [first_name] => Richard
                [patient_first_name] => Donna
                [trip_date] => 2018-08-24
                [request] => Array    
                        [0] => stdClass Object
                        (
                            [id] => 55
                            [client_id] => 10120
                            [city] => PHX
                            [state] => AZ
                            [zip] => 85008
                        )

                        [1] => stdClass Object
                        (
                            [id] => 59
                            [client_id] => 11229
                            [city] => BUCKEYE
                            [state] => AZ
                            [zip] => 85326
                        )
)
    [2] => Array
            (
                [first_name] => Richard
                [patient_first_name] => Donna
                [trip_date] => 2018-08-24
                [request] => Array
                        [0] => stdClass Object
                        (
                            [id] => 69
                            [client_id] => 13769
                            [city] => PHOENIX
                            [state] => AZ
                            [zip] => 85035
                        )

                        [1] => stdClass Object
                        (
                            [id] => 175
                            [client_id] => 16437
                            [city] => Phx
                            [state] => Az
                            [zip] => 85029
                        )

                    )

                )

这是我的代码。

$drivers = [];

        foreach($recs as $val => $rec) {
            $drivers[$rec->driver_id]['first_name'] = $rec->first_name;
            $drivers[$rec->driver_id]['patient_first_name'] = $rec->patient_first_name;
            $drivers[$rec->driver_id]['trip_date'] = $rec->trip_date;
            $drivers[$rec->driver_id]['request'][] = $rec;
        }

        foreach($drivers as $val => $driver) {
            $drivers = array_chunk($driver['request'], 2);
        }

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

根据需要使用array-chunk。检查以下示例(为简化起见,我删除了一些数据):

$request = array(["id" => 46], ["id" => 49], ["id" => 55], ["id" => 59], ["id" => 69], ["id" => 175], ["id" => 195], ["id" => 197]);
$arr[] = array("first_name" => "Richard", "request" => $request);
foreach($arr as $driver) {
    $requests = array_chunk($driver['request'], 2);
    foreach($requests as $chunck) {
        $ans[] = array("id" => $driver["first_name"], "request" => $chunck); // here you  can add all the other data you need from the "driver" object
    }
}

现在,$ans将显示您的需求

答案 1 :(得分:1)

从源数组中获取“请求”,对其进行分块,然后将其余项添加到结果数组的每个元素中

$res = array_chunk($recs['request'], 2);
unset($recs['request']);

foreach($res as &$x)  {
   $x += $recs;
}