Vaidikalaya

Divide the array into two equal parts


In PHP, There are two methods to divide an array into two equal parts.

1. array_chunk
2. array_slice

1. Using array_chunk method
Syntax

array_chunk(array, size, preserve_key)

Example
<?php
$fruits=['Apple', 'Banana', 'Orange', 'Grapes', 'Mango', 'Strawberry'];
print_r(array_chunk($fruits,3));
?>

2. Using array_slice method
Syntax

array_slice(array, start, length, preserve)

Example
<?php
$fruits=['Apple', 'Banana', 'Orange', 'Grapes', 'Mango','Strawberry'];
$tempArray[]=array_slice($fruits,0,3);
$tempArray[]=array_slice($fruits,3,5);
print_r($tempArray);
?>