Search code examples
phparraysmultidimensional-arrayphp-5.3

array_chunk'ing a multi-dimensional array in PHP


I have a multi-dimensional array and I am looking to split it up every 10 elements.

array_chunk appeared to be the solution, but I can't find anything that suggests it supports multi-dimensional arrays.

Is this possible using PHP 5.3?

This is a sample of my current array:

Array
(
    [0] => Cart Object
        (
            [cartId:protected] => 4337032
            [userId:protected] => 271561
            [orderId:protected] => 1042104
        )
    [1] => Cart Object
        (
            [cartId:protected] => 4337032
            [userId:protected] => 271561
            [orderId:protected] => 1042104
        )
    ... and so on ...
)

What i'm looking for:

Array
(
    [0] => 
          Array (
                 [0] => Cart Object
                 (
                    [cartId:protected] => 4337032
                    [userId:protected] => 271561
                    [orderId:protected] => 1042104
                 )
                 [1] => Cart Object
                 (
                    [cartId:protected] => 4337032
                    [userId:protected] => 271561
                    [orderId:protected] => 1042104
                )
    [1] => // the next 10...
)

Solution

  • $carts_to_page = array_chunk($carts, 10);