12

I have an array of objects, each keyed by a unique random ID.

111 => 
    object(stdClass)[452]
      public 'Description' => string 'Description here...' (length=728)       
      public 'Name' => string 'Shirt' (length=18)
      public 'Price' => float 36.56

222 => 
    object(stdClass)[452]
      public 'Description' => string 'Description here...' (length=728)       
      public 'Name' => string 'Pants' (length=18)
      public 'Price' => float 36.56

333 => 
    object(stdClass)[452]
      public 'Description' => string 'Description here...' (length=728)       
      public 'Name' => string 'Dress' (length=18)
      public 'Price' => float 36.56

444 => 
    object(stdClass)[452]
      public 'Description' => string 'Description here...' (length=728)       
      public 'Name' => string 'Dress' (length=18)
      public 'Price' => float 36.56

...

My goal is to split my keyed arrays of objects into chunks of 2 for pagination purposes. So something like this would do:

0 =>
    111 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)           
          public 'Name' => string 'Shirt' (length=18)
          public 'Price' => float 36.56

    222 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)           
          public 'Name' => string 'Pants' (length=18)
          public 'Price' => float 36.56
1 =>
    333 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)           
          public 'Name' => string 'Dress' (length=18)
          public 'Price' => float 36.56

    444 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)          
          public 'Name' => string 'Dress' (length=18)
          public 'Price' => float 36.56
...

My problem is by using array_chunk() to split up my arrays of objects into groups of 2, my unique ID's are not being preserved.

private function paginate($array)
{
    $chunks = 2;
    $paginatedResults = array_chunk($array, $chunks);

    return $paginatedResults;
}

Function Output:

0 =>
    0 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)
          public 'Name' => string 'Shirt' (length=18)
          public 'Price' => float 36.56

    1 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)
          public 'Name' => string 'Pants' (length=18)
          public 'Price' => float 36.56
1 =>
    0 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)
          public 'Name' => string 'Dress' (length=18)
          public 'Price' => float 36.56

    1 => 
        object(stdClass)[452]
          public 'Description' => string 'Description here...' (length=728)
          public 'Name' => string 'Dress' (length=18)
          public 'Price' => float 36.56
...

How can I split up my keyed array of objects into another array containing 2 objects per index while preserving my original array keys containing the unique ID?

1

2 Answers 2

39

All I had to do was set the third parameter of array_chunk() to true like so:

$paginatedResults = array_chunk($array, $chunk, true);
Sign up to request clarification or add additional context in comments.

Comments

9

Seems like the third parameter of array array_chunk ( array $input , int $size [, bool $preserve_keys = false ] ) controls exactly that.

<?php
$x = array_flip(range('a','j'));
var_dump($x);
var_dump(array_chunk($x, 3, true));

prints

array(10) {
  ["a"]=>
  int(0)
  ["b"]=>
  int(1)
  ["c"]=>
  int(2)
  ["d"]=>
  int(3)
  ["e"]=>
  int(4)
  ["f"]=>
  int(5)
  ["g"]=>
  int(6)
  ["h"]=>
  int(7)
  ["i"]=>
  int(8)
  ["j"]=>
  int(9)
}
array(4) {
  [0]=>
  array(3) {
    ["a"]=>
    int(0)
    ["b"]=>
    int(1)
    ["c"]=>
    int(2)
  }
  [1]=>
  array(3) {
    ["d"]=>
    int(3)
    ["e"]=>
    int(4)
    ["f"]=>
    int(5)
  }
  [2]=>
  array(3) {
    ["g"]=>
    int(6)
    ["h"]=>
    int(7)
    ["i"]=>
    int(8)
  }
  [3]=>
  array(1) {
    ["j"]=>
    int(9)
  }
}

Comments

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.