1

How can I flatten a two dimensional array into one dimensional array?

For example:

Array
(
    [1] => Array
        (
            [key] => val
            [key2] => val2
        )
)

Becomes:

Array
(
   [key] => val
   [key2] => val2
)
Josh
  • 13
  • 2

5 Answers5

4

for your example:

$myarray = array_shift($myarray);

or

$myarray = $myarray[1];

but:

could there be more than one sub-array?

if so: do this sub-arrays have keys with the same name?

if so: what should happen with the duplicates? rename them all? drop all but one?

as you can see, you'll have to give some more information on this. the question really isn't clear.

oezi
  • 51,017
  • 10
  • 98
  • 115
1
$array = array_shift($array);   

this will take care of key also, its not necessary all array start with 0 or 1 or anything.

Gaurav
  • 28,447
  • 8
  • 50
  • 80
  • Accessing `$array[1]` would probably be simpler in that case. – Arc Mar 30 '11 at 14:25
  • 1
    @Archimedix: Not all arrays start with 1 as the first index, so array_shift makes it completely independent of the key and simply pops the first element off the array. – Marc B Mar 30 '11 at 14:34
  • @Marc B, I know, but the answer specifically stated `// for your example only` and was mentioned to be suitable, therefore `$array[1]` is just as suitable. Your argument is like saying that not all arrays are stored in variables named `$array`, which is correct but should hopefully be obvious enough. The entire example only works if the outer array consists of 1 element only and is thus highly specific. – Arc Mar 30 '11 at 23:09
  • if you don't know the key of the first element: `$array = reset($array);`. In any case this answer is very limited to the array in question only and does not reflect the general character of a Q&A. The warning note has been even removed. – hakre Jul 12 '15 at 07:56
1

One obvious way would be to foreach over the array (preferrably by reference, to save copying all data over and over), and array_combine them into a new array.

Something like this:

$b = array();
foreach($arr as &$a) $b = array_combine($b, $a);
$arr = $b;

Though as others point out, in your particular special case array_shift is sufficient.

Damon
  • 67,688
  • 20
  • 135
  • 185
  • Arrays are automatically passed by reference these days. No reason to specify it. Oh, objects are automatically passed by reference as well. – Phoenix Mar 30 '11 at 14:40
  • Hmm... in my understanding, the wording of the `foreach` docs says the opposite: "Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself". Do you have a reference for that (no pun intended), especially which versions do auto-referencing? – Damon Mar 30 '11 at 14:48
  • Hmm, might be wrong then, at least with foreach loops. You can test it for other purposes though. You can do something like `function test($array){$array[0] = 2;} $test = array(1,2,3); test($test); print_r($test);` and you should get array(2,2,3) even though it wasn't passed by reference. Even happens if you do `$var = $test; $test[0] = 2; print_r($var);` and you'll get array(2,2,3) from `$var` even though you only changed the value of `$test[0]`. I think they changed it in 5.2 or somewhere around there. – Phoenix Mar 30 '11 at 14:58
  • Damn, maybe I'm wrong again. I know it happens with objects. Quite annoying. – Phoenix Mar 30 '11 at 15:07
0

array_shift

Alex
  • 7,320
  • 1
  • 18
  • 31
0

Assuming that your keys are unique

$oneDArray = array();
foreach($multiDimensionalArray as $k=>$v) {
    $oneDArray[$k]=$v;
}
unset($multiDimensionalArray); //If you don't want to keep it.
Belinda
  • 1,230
  • 2
  • 14
  • 25