3

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?

2
  • 1
    ...sorry, \I had to go away just after commenting on your now-deleted question. I am happy to see that you got array_chunk() to work. It is the best tool for the job. (this question is written much better/clearer than your earlier post) Commented Jan 31, 2019 at 23:23
  • Why, in your script, does it look like ypu can access driver_id as an object property, but $rec doesn't have driver_id (it has client_id). Is client_id what you are grouping on? Then batching every two requests per group? Commented Dec 25, 2024 at 6:26

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

That was only example - you can ignore the first 2 line of my code and use your array
1

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

1 Comment

$drivers would the source I made some changes but I am getting an error back saying expects parameter 1, the function clearly does have a value. $res = array_chunk($drivers['request'], 2); unset($drivers['request']); foreach($res as &$x) { $x += $drivers; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.