How do I flatten an associative array into an array with only values in PHP?

14,376

Solution 1

Your own solution is probably the cleanest solution, so converting it to a "one-liner":

$array = array('name' => 'aalaap','age' => 29, 'location' => 'mumbai');
$answer = array();

array_walk($array, create_function('$val,$key', 'global $answer; $answer[]=$key; $answer[]=$val;'));

var_dump($answer);

This avoids unnecessary and expensive array copies or sorting.

Alternatively, lose the global:

array_walk($array, create_function('$val,$key,$result', '$result[]=$key; $result[]=$val;'), &$answer);

Solution 2

PHP 5.3+ version of Just Jules' answer, and a bit more readable:

array_walk($array, function($val, $key) use (&$answer) {
    $answer[] = $key;
    $answer[] = $val;
});

Solution 3

I don't think this is possible - with the built-in functions you'll end up with all the keys then all the values:

$a = array('a' => 'A', 'b' => 'B', 'c' => 'C');

$a = array_merge(array_keys($a), array_values($a));
print_r($a);

You're going to have to use a loop like this:

$b = array();
foreach ($a as $key => $value)
{
    $b[] = $key;
    $b[] = $value;
}

Solution 4

It is possible, but I don't think it is more readable or any faster. It would work with a less-known feature of PHP - the array addition:

$array = array('name' => 'aalaap', 'age' => 29, 'location' => 'mumbai');

# Separate keys and values into distinct arrays
$keys = array_keys($array);
$values = array_values($array);

# Generate 2 new array containing indexes for the 2 arrays which contain
# only odd/even numbers:
$keysKeys = range(0, count($keys) * 2 - 1, 2);
$valuesKeys = range(1, count($keys) * 2, 2);

# Combine the keys with the values and add the results:
$array = array_combine($keysKeys, $keys) + array_combine($valuesKeys, $values);

# Sort the resulting array, otherwise the numbering will be broken
# (1,3,5,2,4,6)
ksort($array);

# Result:
var_dump($array);
array(6) {
    [0]=>
        string(4) "name"
    [1]=>
        string(6) "aalaap"
    [2]=>
        string(3) "age"
    [3]=>
        int(29)
    [4]=>
        string(8) "location"
    [5]=>
        string(6) "mumbai"
}
Share:
14,376
aalaap
Author by

aalaap

I build things.

Updated on June 22, 2022

Comments

  • aalaap
    aalaap almost 2 years

    I have an array that has keys and values. For eg:

    Array (
        [name] => aalaap
        [age] => 29
        [location] => mumbai
    )
    

    I want to convert the keys from this into values, but I want the values to apear right after the keys. For eg:

    Array (
        [0] => name
        [1] => aalaap
        [2] => age
        [3] => 29
        [4] => location
        [5] => mumbai
    )
    

    I can easily write an iteration function that will do this... for eg:

    array_flatten($arr) {
        foreach ($arr as $arrkey => $arrval) {
            $arr_new[] = $arrkey;
            $arr_new[] = $arrval;
        }
        return $arr_new;
    } 
    

    ...but I'm trying to find out if there's any way this can be accomplished using array_combine, array_keys, array_values and/or array_merge, preferably in one, so i don't need to use a custom function.

    Is there?

    • jbasko
      jbasko almost 15 years
      I wonder why PHP does not have a function in global namespace called array_flatten.
    • aalaap
      aalaap almost 15 years
      Do you think this is worth including in PHP natively?
  • aalaap
    aalaap almost 15 years
    this is just too much code for something that can already be done in two lines!
  • aalaap
    aalaap almost 15 years
    I like this! My array_flatten function is probably a bit more intuitive to use, but I like this method! Thanks!
  • aalaap
    aalaap almost 15 years
    Kinda! I should have said <em>"<strong>easily</strong> accomplished"</em> :-)
  • Willem Van Onsem
    Willem Van Onsem almost 10 years
    Are such functions "compiled". Or are they processed at each single step?