2012-11-20 99 views
7

我有一個多維數組下面,我想通過它循環,並將[menu_cats]的值從數字更改爲正在從數據庫選擇拉出的字符串。這可能嗎?數組的名稱是'result'。PHP循環通過多維數組和更改值

Array 
(
[0] => Array 
    (
     [0] => Array 
      (
       [menu_cats] => 1      
       [item] => Introduction 
       [link] => needs 
      ) 

     [1] => Array 
      (
       [menu_cats] => 1 
       [item] => Needs Assessment 
       [link] => needs/needs.php 
      ) 

    ) 

[1] => Array 
    (
     [0] => Array 
      (
       [menu_cats] => 2      
       [item] => Introduction 
       [link] => knowledge 
      ) 

     [1] => Array 
      (
       [menu_cats] => 2 
       [item] => Administer Knowledge Pre-Test 
       [link] => knowledge/pre_test.php 
      ) 
    ) 

) 

回答

11
foreach($result as $key => $subarray) { 
    foreach($subarray as $subkey => $subsubarray) { 
     $result[$key][$subkey]['menu_cats'] = 'your string here'; 
    } 
} 
+0

完美!謝謝! – stevenpepe

+2

會有很好的遞歸功能...比多層次的作品 – gagarine