PHP循环遍历多维数组并更改值

时间:2012-11-20 16:55:38

标签: php arrays loops

我下面有一个多维数组,我想循环遍历它并将[menu_cats]的值从数字更改为字符串,该字符串是从数据库选择中提取的。这可能吗?数组的名称是'result'。

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
            )
    )

)

1 个答案:

答案 0 :(得分:11)

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