I have an array which is multidimensional for no reason
/* This is how my array is currently */
Array
(
[0] => Array
(
[0] => Array
$singleArray = array();
foreach ($multiDimensionalArray as $key => $value){
$singleArray[$key] = $value['plan'];
}
this is best way to create a array from multiDimensionalArray array.
thanks
Despite that array_column
will work nice here, in case you need to flatten any array no matter of it's internal structure you can use this array library to achieve it without ease:
$flattened = Arr::flatten($array);
which will produce exactly the array you want.
Just assign it to it's own first element:
$array = $array[0];
Assuming this array may or may not be redundantly nested and you're unsure of how deep it goes, this should flatten it for you:
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}
This simple code you can use
$array = array_column($array, 'value', 'key');
For this particular case, this'll do:
$array = array_map('current', $array[0]);
It's basically the exact same question is this one, look at some answers there: PHP array merge from unknown number of parameters.