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

April 11, 2018 —John Koster

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.

#Signature

The signature of the divide method is:

1public static function divide(
2 $array
3);

#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;
2 
3$myArray = [
4 'animal' => 'Araripe Manakin',
5 'plant' => 'Pineland Wild Petunia'
6];
7 
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 }
10}

#Global array_divide Helper Function

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

Some absolutely amazing
people

The following amazing people help support this site and my open source projects ♥️
If you're interesting in supporting my work and want to show up on this list, check out my GitHub Sponsors Profile.