2017-07-06 110 views
0

我必須用php迭代多維數組。我的陣列數據如下:迭代多級多維數組

Array 
(

[id] => Array 
    (
     ['2'] => Array 
      (
       [0] => 2 
      ) 

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

    ) 

[summary] => Array 
    (
     ['2'] => Array 
      (
       [0] => Summary 1 
      ) 

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

    ) 

[review] => Array 
    (
     ['2'] => Array 
      (
       [0] => Review 2 
      ) 

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

    ) 

[nickname] => ABCD 
) 

我想要的結果集像array('id','summary','review', 'nickname')

+0

這裏的任何更新? :) – sv3n

回答

1

你可以試試這個:

$array = array(
    'id'  => array(2 => array(2), 1 => array(1)), 
    'summary' => array(2 => array('Summary ' . 1), 1 => array('Summary ' . 2)), 
    'review' => array(2 => array('Review ' . 2), 1 => array('Review ' . 2)), 
    'nickname' => 'ABCD' 
); 

$out = array(); 
foreach ($array as $col => $data) { 
    if (is_array($data)) { 
     foreach ($data as $key => $value) { 
      $out[$key][$col] = $value[0]; 
     } 
    } 
} 

foreach ($out as &$data) { 
    $data += array('nickname' => $array['nickname']); 
} 

var_dump($out); 

輸出:

array (size=2) 
    2 => 
    array (size=4) 
     'id' => int 2 
     'summary' => string 'Summary 1' (length=9) 
     'review' => string 'Review 2' (length=9) 
     'nickname' => string 'ABCD' (length=4) 
    1 => & 
    array (size=4) 
     'id' => int 1 
     'summary' => string 'Summary 2' (length=9) 
     'review' => string 'Review 2' (length=9) 
     'nickname' => string 'ABCD' (length=4)