-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
  • 37,596
  • 11
  • 75
  • 105
Abdan Syakuro
  • 914
  • 2
  • 9
  • 25

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,227
  • 1
  • 8
  • 24
-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
  • 551
  • 5
  • 9