Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve original unique array keys while using array_chunk?

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?

like image 312
Fetus Avatar asked Dec 03 '14 16:12

Fetus


People also ask

How to get same Value in array in PHP?

The array_intersect() function compares the values of two (or more) arrays, and returns the matches. This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.

What is array_ chunk()?

The array_chunk() function takes an array as input and split that array into smaller chunks of the given size. The last chunk may contain less number of elements than passed size based on the multiplicity factor of the total numbers available in the array.

How can I split an array into two parts in PHP?

PHP: Split an array into chunksThe array_chunk() function is used to split an array into arrays with size elements. The last chunk may contain less than size elements. Specifies the array to split. If we set preserve_keys as TRUE, array_chunk function preserves the original array keys.

What function allows you to easily split up an array into pieces?

The array_chunk() function splits an array into chunks of new arrays.


2 Answers

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

$paginatedResults = array_chunk($array, $chunk, true);
like image 86
Fetus Avatar answered Sep 30 '22 19:09

Fetus


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)
  }
}
like image 31
VolkerK Avatar answered Sep 30 '22 20:09

VolkerK