how to split the array in to two equal parts using php how to split the array in to two equal parts using php php php

how to split the array in to two equal parts using php


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);


$array1 = array_slice($array, 0, 1199);$array2 = array_slice($array, 1200);