2013-08-30 172 views
0

所以我目前有一個看起來像這樣的數組。分組多維數組

Array 
(
    [name] => Array 
     (
      [1] => Array 
       (
        [0] => 500aqua.jpg 
       ) 

      [2] => Array 
       (
        [0] => 500beige.jpg 
        [1] => 500beige-zoom.jpg 
       ) 

      [3] => Array 
       (
        [0] => P1040485.JPG 
        [1] => P1040486.JPG 
        [2] => P1040487.JPG 
       ) 
     ) 

    [type] => Array 
     (
      [1] => Array 
       (
        [0] => image/jpeg 
       ) 

      [2] => Array 
       (
        [0] => image/jpeg 
        [1] => image/jpeg 
       ) 

      [3] => Array 
       (
        [0] => image/jpeg 
        [1] => image/jpeg 
        [2] => image/jpeg 
       ) 
     ) 

    [tmp_name] => Array 
     (
      [1] => Array 
       (
        [0] => C:\xampp\tmp\php5F9.tmp 
       ) 

      [2] => Array 
       (
        [0] => C:\xampp\tmp\php5FA.tmp 
        [1] => C:\xampp\tmp\php5FB.tmp 
       ) 

      [3] => Array 
       (
        [0] => C:\xampp\tmp\php60C.tmp 
        [1] => C:\xampp\tmp\php63B.tmp 
        [2] => C:\xampp\tmp\php66B.tmp 
       ) 
     ) 

    [error] => Array 
     (
      [1] => Array 
       (
        [0] => 0 
       ) 

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

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

    [size] => Array 
     (
      [1] => Array 
       (
        [0] => 133659 
       ) 

      [2] => Array 
       (
        [0] => 148940 
        [1] => 211420 
       ) 

      [3] => Array 
       (
        [0] => 3939511 
        [1] => 4742470 
        [2] => 4345899 
       ) 
     ) 

) 

而且我希望它看起來像這樣

Array 
(
    [0] => Array 
     (
      [0] => Array 
       (
        [name] => 500aqua.jpg 
        [type] => image/jpeg 
        [tmp_name] => C:\xampp\tmp\php5F9.tmp 
        [error] => 0 
        [size] => 133659 

       ) 
     ) 
    [1] => Array 
     (
      [0] => Array 
       (
        [name] => 500beige.jpg 
        [type] => image/jpeg 
        [tmp_name] => C:\xampp\tmp\php5FA.tmp 
        [error] => 0 
        [size] => 148940 
       ) 
      [1] => Array 
       (
        [name] => 500beige-zoom.jpg 
        [type] => image/jpeg 
        [tmp_name] => C:\xampp\tmp\php5FB.tmp 
        [error] => 0 
        [size] => 211420 
       ) 
     ) 
    [2] => Array 
     (
      [0] => Array 
       (
        [name] => P1040485.JPG 
        [type] => image/jpeg 
        [tmp_name] => C:\xampp\tmp\php60C.tmp 
        [error] => 0 
        [size] => 148940 
       ) 
      [1] => Array 
       (
        [name] => P1040486.JPG 
        [type] => image/jpeg 
        [tmp_name] => C:\xampp\tmp\php63B.tmp 
        [error] => 0 
        [size] => 148940 
       ) 
      [2] => Array 
       (
        [name] => P1040487.JPG 
        [type] => image/jpeg 
        [tmp_name] => C:\xampp\tmp\php66B.tmp 
        [error] => 0 
        [size] => 148940 
       ) 
     ) 
) 

我嘗試下面的代碼,但它似乎只得到每個最後3中的陣列。

foreach ($data as $d) { 
    foreach ($d as $key => $e) { 
     foreach ($e as $key2 => $f) { 
      $result[$key2][$key] = $f; 
     } 
    } 
} 

等預先

Array 
(
    [0] => Array 
     (
      [name] => P1040485.JPG 
      [type] => image/jpeg 
      [tmp_name] => C:\xampp\tmp\php60C.tmp 
      [error] => 0 
      [size] => 3939511 
     ) 

    [1] => Array 
     (
      [name] => P1040486.JPG 
      [type] => image/jpeg 
      [tmp_name] => C:\xampp\tmp\php63B.tmp 
      [error] => 0 
      [size] => 4742470 
     ) 

    [2] => Array 
     (
      [name] => P1040487.JPG 
      [type] => image/jpeg 
      [tmp_name] => C:\xampp\tmp\php66B.tmp 
      [error] => 0 
      [size] => 4345899 
     ) 

) 

感謝。

回答

1

試試這個:

foreach ($data as $key1 => $value1) { 
    foreach ($value1 as $key2 => $value2) { 
     foreach ($value2 as $key3 => $value3) { 
      $result[$key2][$key3][$key1] = $value3; 
     } 
    } 
} 

你忘了,包括第一級密鑰($ KEY1):名稱,類型等

+0

輝煌,傻我。 –

1

試試這個:

foreach ($data as $field => $field_values) { 
    foreach (array_values($field_values) as $index_1 => $index_1_values) { 
     foreach ($index_1_values as $index_2 => $value) { 
      $result[$index_1][$index_2][$field] = $value; 
     } 
    } 
}