This question already has an answer here:
I want to merge an array with the current output as shown below to the expected output.
I was using a function array_reduce($currentArray, 'array_merge', array());
and it was working perfectly fine. But it doesnt seem to work now.
Current Output:
Array
(
[0] => Array
(
[0] => Array(...),
[1] => Array(...),
[2] => Array(...),
)
[1] => Array
(
[0] => Array(...),
[1] => Array(...),
[2] => Array(...), "
)
)
Expected Output:
Array
(
[0] => Array(...),
[1] => Array(...),
[2] => Array(...),
[3] => Array(...),
[4] => Array(...),
[5] => Array(...),
)
</div>
best Solution I Have Used earlier
DEMO
<?php
$array = array(
'0' => array("1","2","3"),
'1' => array("1","2","3"),
'2' => array("1","2","3"),
);
echo "<pre>";
print_r($array);
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;
}
echo "<pre>";
print_r(array_flatten($array));
You can try this: call_user_func_array
$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);
print_r( $oneDimensionalArray);
Give this a try.
$final = array();
foreach($currentArray as $sub_array) {
$final = array_merge($final, $sub_array);
}
foreach loop will loop through the array and array merge will merge the content array with the $final
array.
1. call_user_func_array : Call a callback with an array of parameters
2. array_reduce : Iteratively reduce the array to a single value using a callback function
<?php
$array = [
[
[
"name" ,"email", "mobile"
],
[
"name1" ,"email1", "mobile1"
]
],
[
[
"name2" ,"email2", "mobile2"
],
[
"name3" ,"email3", "mobile3"
]
],
];
$flat = call_user_func_array('array_merge', $array);
$flat = array_reduce($array, 'array_merge', array());
echo "<pre>";
print_r($flat);