Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP loop through multidimensional array and change values

Tags:

arrays

loops

php

I have a multidimensional array below and I want to loop through it and change the value of [menu_cats] from a number to a string, which is being pulled from a database selection. Is this possible? The name of the array is '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
            )
    )

)
like image 846
stevenpepe Avatar asked Nov 20 '12 16:11

stevenpepe


1 Answers

foreach($result as $key => $subarray) {
   foreach($subarray as $subkey => $subsubarray) {
      $result[$key][$subkey]['menu_cats'] = 'your string here';
   }
}
like image 128
Marc B Avatar answered Sep 24 '22 03:09

Marc B