I have an array of objects, each of which has a unique random identifier.
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 arrays with object keys into pieces 2 for pagination purposes. So something like this:
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 to use array_chunk()to separate arrays of objects into groups of 2, my unique identifier is not saved.
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 my array with a key into another array containing 2 objects per index, keeping my original array keys containing a unique identifier?
source
share