-1

I have a problem in my array.

My array looks like this:

[[1,2,3,4],[5,6,7],[8,9,10,11,12]]

I have tried using array_merge() to merge into one array.

I have tried to merge array with array_combine(), but it still don't merge.

I want my array becomes like this:

[1,2,3,4,5,6,7,8,9,10,11,12]
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Abdan Syakuro
  • 1,034
  • 2
  • 12
  • 26

2 Answers2

0
function recursive_merge_array($array) {
  $resArray = array();
  $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($array));
  foreach($it as $v) {
    $resArray[] = $v;
  }

  return $resArray;
}

$myArrays = [[1,2,3,4],[5,6,7],[8,9,10,11,12]];
$mergedArrays = recursive_merge_array($myArrays);
print_r($mergedArrays);
Chol Nhial
  • 1,327
  • 1
  • 10
  • 25
-1

This works, but i'm not 100% if its the best solution for this problem...

$orignalArray = [[1,2,3,4],[5,6,7],[8,9,10,11,12]];

$newArray = array();

foreach ($orignalArray as $child){
    $newArray = array_merge($newArray, $child);
}

print_r($newArray);
dougtesting.net
  • 571
  • 4
  • 9