2016-12-29 238 views
0

的內內數組我有以下陣列:合併陣列

Array (  
     [0] => Array 
       (
        [0] => 1 
        [1] => 2 
        [2] => 3 

       ) 

     [1] => Array 
       (
        [0] => Array 
         (
          [0] => 1 
          [1] => 2 
         ) 
        [1] => Array 
         ( 
          [0] => 3 
          [1] => 4 
         ) 
       ) 
    ) 

我想要做的就是內陣列合併在一起[1]給我一個新的陣列看起來是這樣的:

Array ( 
     [0] => Array 
         (
          [0] => 1 
          [1] => 2 
          [2] => 3 

         ) 
     [1] => Array 
         (
          [0] => 1 
          [1] => 2 
          [2] => 3 
          [4] => 4 
         ) 
    ) 

請問我該怎麼做的任何提示?由於

回答

1
<?php 

$arr = array(
    0 => array(
     0 => 1, 
     1 => 2, 
     2 => 3 
    ), 
    1 => array(
     0 => array(
      0 => 1, 
      1 => 2 
     ), 
     1 => array( 
      0 => 3, 
      1 => 4 
     ) 
    ) 
); 

for ($i = 0; $i < count($arr); $i++) { 
    $fixedArray = array(); 

    foreach ($arr[$i] as $key => $value) { 
     if (!is_array($value)) continue; 

     foreach ($value as $subKey => $subValue) 
      $fixedArray[] = $subValue; 
    } 

    if (count($fixedArray) > 0) 
     $arr[$i] = $fixedArray; 
} 

print_r($arr); 

?> 

輸出:

Array 
(
    [0] => Array 
     (
      [0] => 1 
      [1] => 2 
      [2] => 3 
     ) 

    [1] => Array 
     (
      [0] => 1 
      [1] => 2 
      [2] => 3 
      [3] => 4 
     ) 

) 

那是一個辦法做到這一點,並不是說這是最好的方式,但它的工作原理。

+0

感謝,沒有工作! –

1

這個礦:

<?php 
$multiArray = [ 
    0 => [ 
     0 =>1, 
     1 => 2, 
     2 => 3 
    ], 
    1 => [ 
     0 => [ 
      0 => 1, 
      1 => 2 
     ], 
     1 => [ 
      0 => 3, 
      1 => 4 

     ] 
    ] 
]; 

$aux = []; 
foreach($multiArray as $key => $arr){ 
    foreach($arr as $k =>$a){ 
     if (is_array($a)){ 
      $aux= $arr; 
      break; 
     } 
    } 
} 

list($item1, $item2) = $aux; 
$new = array_merge($item1, $item2); 
$multiArray[1] =$new; 
print_r($multiArray); 
?>