Laravel 5

Laravel 5: Splitting an Array Into It's Keys and Values with divide


John Koster

Published on April 11, 2018

The divide helper method will take the given $array and create two new arrays. The first array will be all of the keys from the original $array and the second array will be all of the values.


The signature of the divide method is:

1public static function divide(
2 $array

Example Use

The following examples demonstrate how to use the divide method separate an array into its key and value components:

1use Illuminate\Support\Arr;
3$myArray = [
4 'animal' => 'Araripe Manakin',
5 'plant' => 'Pineland Wild Petunia'
8$myArray = Arr::divide($myArray);

The final result would be:

1array {
2 array {
3 "animal"
4 "plant"
5 }
6 array {
7 "Araripe Manakin"
8 "Pineland Wild Petunia"
9 }

Global array_divide Helper Function

The array_divide function is a shortcut to calling Arr::divide. This function is declared in the global namespace.