Recursively searching and changing a value - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.

Name: Recursively searching and changing a value fullscreencopydownloadembedprint


Your result can be seen below.

Result of php executing





Full code of Recursively searching and changing a value.php

  1. <?php
  2.  
  3. $menuData = [
  4.     [
  5.         ['menu_cats' => 1, 'item' => 'Introduction', 'link' => 'needs'],
  6.         ['menu_cats' => 1, 'item' => 'Needs Assessment', 'link' => 'needs/needs.php']
  7.     ],
  8.     [
  9.         ['menu_cats' => 2, 'item' => 'Introduction', 'link' => 'knowledge'],
  10.         ['menu_cats' => 2, 'item' => 'NeedsAdminister Knowledge Pre-Test', 'link' => 'needsknowledge/pre_test.php']
  11.     ]
  12. ];
  13.  
  14. recursiveArray($menuData);
  15. print_r($menuData);
  16.  
  17. # A recursive function to traverse the $rowData array
  18. function recursiveArray(array &$rowData)
  19. {
  20.     foreach ($rowData as $key => &$hitElement) {
  21.         # If there is a element left
  22.         if (is_array($hitElement)) {
  23.             # call recursive structure to parse the $rowData
  24.             recursiveArray($hitElement);
  25.         } else {
  26.             if ($key == 'menu_cats') {
  27.                 $hitElement = 'new value';
  28.             }
  29.         }
  30.     }
  31. }
File Description
  • Recursively searching and changing a value
  • PHP Code
  • 06 Oct-2022
  • 902 Bytes
You can Share it: