Is there an easy way to split an array into two arrays, one consisting of all the keys and the other consisting of all the values? This would be a reverse to the action of array_combine. Is there an inbuilt function for doing such a task? Let's use an example array:
$array = array('Tiger' => 'Forest', 'Hippo' => 'River', 'Bird' => 'Sky');
Is there a function that will split the above array into:
$array_keys = array('Tiger', 'Hippo', 'Bird');
$array_values = array('Forest', 'River', 'Sky');
Let's use an example array: $array = array('Tiger' => 'Forest', 'Hippo' => 'River', 'Bird' => 'Sky'); Is there a function that will split the above array into: $array_keys = array('Tiger', 'Hippo', 'Bird'); $array_values = array('Forest', 'River', 'Sky');
PHP: Split an array into chunksThe array_chunk() function is used to split an array into arrays with size elements. The last chunk may contain less than size elements. Specifies the array to split. If we set preserve_keys as TRUE, array_chunk function preserves the original array keys.
There are two functions called array_keys and array_values:
$array_keys = array_keys($array);
$array_values = array_values($array);
There are two functions actually:
$keys = array_keys($array);
$values = array_values($array);
You can also do the exact opposite:
$array = array_combine($keys, $values);
use array_keys and array_values
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