-1

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(...), 

)
prakashchhetri
  • 1,722
  • 4
  • 33
  • 59

4 Answers4

3

You can try both ways as shown below

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);
Aman Kumar
  • 4,377
  • 2
  • 17
  • 39
  • I can't believe how many poor answers there is in this thread. Nice to someone doing it properly. – Andreas Jan 09 '18 at 08:12
0

You can try this: call_user_func_array

$oneDimensionalArray = call_user_func_array('array_merge', $twoDimensionalArray);

print_r( $oneDimensionalArray);
Aftab H.
  • 1,517
  • 4
  • 13
  • 25
-1

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.

Annapurna
  • 479
  • 1
  • 6
  • 17
-3

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));
TarangP
  • 2,682
  • 5
  • 20
  • 37
  • Sorry mistakenly post.Look I have Updated Answer – TarangP Jan 09 '18 at 06:50
  • You should always include comments to your answer. It doesn't have to be inline the code but at least something that explains what your code does – Andreas Jan 09 '18 at 08:06