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?