This single line would do that:
$array = array_column($array, 'plan');
The first argument is an array | The second argument is an array key.
For details, go to official documentation: https://www.php.net/manual/en/function.array-column.php.
Answer from Usman Ahmed on Stack OverflowThis single line would do that:
$array = array_column($array, 'plan');
The first argument is an array | The second argument is an array key.
For details, go to official documentation: https://www.php.net/manual/en/function.array-column.php.
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
value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[
value;
}
}
return $result;
}
I think you can use array_reduce() function. For example:
$multi= array(0 => array('t1' => 'test1'),1 => array('t2' => 'test2'),2 => array('t3' => 'test3'),3 => array('t4' => 'test4'));
$single= array_reduce($multi, 'array_merge', array());
print_r($single); //Outputs the reduced aray
You can use as follows :
$newArray = array();
foreach($arrayData as
value) {
foreach($value as $key2 => $value2) {
$newArray[$key2] = $value2;
}
}
Where $arrayData is your DB data array and $newArray will be the result.
array_reduce($array, 'array_merge', array())
Example:
$a = array(array(1, 2, 3), array(4, 5, 6));
$result = array_reduce($a, 'array_merge', array());
Result:
array[1, 2, 3, 4, 5, 6];
The PHP array_mergeDocs function can flatten your array:
$flat = call_user_func_array('array_merge', $array);
In case the original array has a higher depth than 2 levels, the SPL in PHP has a RecursiveArrayIterator you can use to flatten it:
$flat = iterator_to_array(new RecursiveIteratorIterator(new RecursiveArrayIterator($array)), 0);
See as well: How to Flatten a Multidimensional Array?.
You could create a recursive function:
function flatten($arr, &$out, $prefix='') {
$prefix = $prefix ? $prefix . '.' : '';
foreach($arr as $k => $value) {
$key = $prefix . $k;
if(is_array($value)) {
flatten($value, $out, $key);
}
else {
$out[$key] = $value;
}
}
}
You can use it as
$out = array();
flatten($array, $out);
You've got something nice here: http://davidwalsh.name/flatten-nested-arrays-php
There are many ways to do it,Try this way simply
$result = call_user_func_array('array_merge', $array);
echo "<pre>";
print_r($result);
echo "</pre>";
It appears my code was working properly, but instead of passing back in a value split from an array, I was re-passing back in the array.
Example :
$newArray[] = $array;
instead of
$newArray[] = $array[1];
Stupid mistake on my end.
$final_array =array();
foreach ($data as $val)
{
foreach($val as $val2)
{
$final_array[] = $val2;
}
}
function makeArray($finalArray,$element) {
foreach ($element as $key => $value){
if(is_array($value)) makeArray($finalArray,$value);
else $finalArray[] = $value;
}
}
If you want a "general-pourpose" solution, this is the one
Obviously you have to call it, the first time, with $finalArray as an empty Array and $element as your starting array