简体   繁体   中英

Using array_chunk on multi array

I have a multi dimensional array and i am now sure how to use array chunk on my array key request while preserving the information into the new array. I would like to split the array every 2 arrays. I tried using array_chunk inside of a loop but no luck.

Here is my array.

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

            )

        )

This is the array I would like.

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

                    )

                )

This is my code.

$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);
        }

Any suggestions?

Using array-chunk if what you need. Check the following example (I remove some of the data to simplify):

$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
    }
}

Now , $ans will have your desire output

Get 'request' from the source array, chunk it and add rest items to each element of the result array

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM