2011-08-17 91 views
0

我試圖重新構建一個數組,但無法弄清楚如何將其重新排列輸出ID爲關鍵,我想改變這一點:重組多維數組

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

    [1] => Array 
     (
      [0] => 17 
      [1] => about-us 
      [2] => About us 
      [3] => About us 
      [4] => About us content 

     ) 

) 

要這樣:

Array 
(
    [16] => Array 
     (
      [0] => News 
      [1] => News 
      [2] => News 
      [3] => News content 
     ) 

    [17] => Array 
     (
      [0] => about-us 
      [1] => About us 
      [2] => About us 
      [3] => About us content 

     ) 

) 

回答

1

我創建了這個演示腳本。這應該工作。

<?php 

$original = array(0 => array(16, 'News', 'Etc'), 1 => array(35, 'Be', 'Here')); 

foreach($original as $key => $value) { 
    $new = array_shift($value); 
    $newarray[$new] = $value; 
} 

echo '<pre>'.print_r($original,1).'</pre>'; 
echo '<pre>'.print_r($newarray,1).'</pre>'; 
?> 

輸出:

Array 
(
[0] => Array 
    (
     [0] => 16 
     [1] => News 
     [2] => Etc 
    ) 

[1] => Array 
    (
     [0] => 35 
     [1] => Be 
     [2] => Here 
    ) 

) 

Array 
(
[16] => Array 
    (
     [0] => News 
     [1] => Etc 
    ) 

[35] => Array 
    (
     [0] => Be 
     [1] => Here 
    ) 

) 
+0

謝謝你的工作非常漂亮。 – Xorp25