2017-03-03 64 views
1

我要轉換在2- d陣列的多陣列。我有以下多數組的結果。轉換多陣列在2- d陣列PHP

Array 
(
[1] => Array 
    (
     [0] => Array 
      (
       [0] => Id 
       [1] => Name 
       [2] => Fname 
       [3] => School 
       [4] => Photo 
      ) 

    ) 

[2] => Array 
    (
     [0] => Array 
      (
       [0] => 32 
       [1] => kamal 
       [2] => hjhbg 
       [3] => hnp 
       [4] => B612_16.jpg 
      ) 

    ) 

[3] => Array 
    (
     [0] => Array 
      (
       [0] => 33 
       [1] => dg 
       [2] => fa 
       [3] => f 
       [4] => bg.jpg 
      ) 

    ) 

[4] => Array 
    (
     [0] => Array 
      (
       [0] => 35 
       [1] => mohit 
       [2] => bc 
       [3] => jhbvj 
       [4] => B612.jpg 
      ) 

    ) 

) 

現在我需要以下面的格式轉換這個數組。

Array 
(
[0] => Array 
    (
       [0] => Id 
       [1] => Name 
       [2] => Fname 
       [3] => School 
       [4] => Photo 

    ) 

[1] => Array 
    (
       [0] => 32 
       [1] => kamal 
       [2] => hjhbg 
       [3] => hnp 
       [4] => B612_16.jpg 

    ) 

[2] => Array 
    (
       [0] => 33 
       [1] => dg 
       [2] => fa 
       [3] => f 
       [4] => bg.jpg 

    ) 

[3] => Array 
    (
       [0] => 35 
       [1] => mohit 
       [2] => bc 
       [3] => jhbvj 
       [4] => B612.jpg 
    ) 

    ) 
+2

的可能的複製[轉換多維數組融入單身陣列(http://stackoverflow.com/questions/6785355/convert-multidimensional-array-into-single-array) –

+0

沒有它的不同@ B.Desai – Ramkishan

回答

1

試試這個

function array_to1d($a) { 
    $out = array(); 
    foreach ($a as $b) { 
     foreach ($b as $c) { 
      if (isset($c)) { 
       $out[] = $c; 
      } 
     } 
    } 
    return $out; 
} 
    echo "<pre>"; print_r(array_to1d($array)); // $array your array name 
0

你只需要做

$two_d_array = array_values($three_d_array); 
+0

對不起薩格爾,但它不工作 – Ramkishan

0

最短的解決辦法是使用array_walk()這裏:

array_walk($array, function(&$v) { 
    $v = $v[0]; 
});