20

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');
1
  • Looked up the functions, used them and they work. So all the others are correct. I guess, I will be spending time with the PHP manual or any reference book. Thanks VERY MUCH to you all.
    – Bululu
    Jun 4, 2011 at 4:57

6 Answers 6

39

There are two functions called array_keys and array_values:

$array_keys = array_keys($array);
$array_values = array_values($array);
2
  • 4
    But will it be absolutely the same order between the keys and values after they are separated? For any source array?
    – datasn.io
    Jan 13, 2014 at 12:50
  • I believe so. For array_values the php documentation explicitly states 'array_values() returns all the values from the array and indexes the array numerically'. As of 28/12/2017 I did not see such an explicit statement for array_keys. However, in the user contributed notes following the help text on the php website, a use of array_keys twice to get the position of an element (contributed by vesely at tana dot it) implies that array_keys returns key element in a similar way to array_values.
    – KolaB
    Dec 28, 2017 at 17:02
9

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

use array_keys and array_values

3

Strangely enough, the functions you're looking for are called array_keys() and array_values().

$keys = array_keys($array);
$vals = array_values($array);
2

Unfortunately there is no built-in inverse of array_combine. There is also no way to define one, since array_combine expects multiple parameters and we can't return multiple values from a function.

We can construct an alternative to array_combine which takes a single argument: the array of keys and the array of values wrapped up together in another array. This transformation is called "uncurrying" and is performed by the "call_user_func_array" function:

$array_comb  = function($arr) { return call_user_func_array('array_combine', $arr); };

This alternative function does have an inverse:

$array_split = function($arr) { return array(array_keys($arr), array_values($arr)); };

If we define function composition:

$compose  = function($f, $g) {
    return function($x) use ($f, $g) { return $f($g($x)); };
};

Then the following functions are all (extensionally) equal, ie. they all return their argument unchanged:

$identity      = function($x) { return $x; };
$left_inverse  = $compose($array_split, $array_comb);  // Split then combine
$right_inverse = $compose($array_comb, $array_split);  // Combine then split

Note that they accept different argument types though:

  • $identity will work on anything.
  • $left_inverse will work on any array.
  • $right_inverse will work on arrays-of-arrays, where the outer array contains 2 elements, both inner arrays are of equal length and the first inner array only contains integers and strings.
1
function array_split($data)
{
  $x = 0;//Counter to ensure accuracy
  $retArray[0] = array();//Array of Keys
  $retArray[1] = array();//Array of Values

  foreach($data as $key => $value)
  {
    $retArray[0][$x] = $key;
    $retArray[1][$x] = $value;
    $x++;
  }

  RETURN $retArray;
}

$data = array("key" => "value", "key2" => "value2");

$splitData = array_split($data);

//print_r($splitData[0]);//Output: Array ( [0] => key [1] => key2 ) 
//print_r($splitData[1]);//Output: Array ( [0] => value [1] => value2 ) 

print_r($splitData);
//Output:
/*
Array
(
    [0] => Array
        (
            [0] => key
            [1] => key2
        )

    [1] => Array
        (
            [0] => value
            [1] => value2
        )

)
*/
1
  • You should explain your answer rather than just pasting the code
    – TResponse
    Feb 15, 2015 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.