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
)
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.
$array = array_shift($array);
this will take care of key also, its not necessary all array start with 0 or 1 or anything.
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.
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.