how to split an array in to two equal parts using array_slice() in PHP ?
First array contains: 0-1200
Second array contains: 1200-end
From the documentation for array_slice, all you have to do is give array_slice
an offset and a length.
In your case:
$firsthalf = array_slice($original, 0, 1200);
$secondhalf = array_slice($original, 1200);
In other words, this code is telling array_slice
:
take the first 1200 records;
then, take all the records starting at index 1200;
Since index 1200 is item 1201, this should be what you need.
$quantity = count($original_collection);
$collection1 = array_slice($original_collection, 0, intval($quantity / 2), true);
$collection2 = array_diff_key($original_collection, $collection1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With