recursive - 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.



Your result can be seen below.

Result of php executing





Full code of recursive.php

  1. <?php
  2.  
  3. $rowData = [
  4.     ['Buffalo', 'Tampa Bay', -7, 'favorite', 0, 46],
  5.     ['Minnesota', 'Tennessee', 3, 'favorite', 1, 33],
  6.     ['Green Bay', 'Cincinnati', 3, 'favorite', 1, 33],
  7.     ['Jacksonville Bay', 'Buffalo', 4, 'underdog', 1, 54],
  8. ];
  9.  
  10. recursiveArray($rowData);
  11. print_r($rowData);
  12.  
  13. # A recursive function to traverse the $rowData array
  14. function recursiveArray(array $rowData)
  15. {
  16.     foreach ($rowData as $key => $hitElement) {
  17.         # If there is a element left
  18.         if (is_array($hitElement)) {
  19.             # call recursive structure to parse the $rowData
  20.             recursiveArray($hitElement);
  21.         } else {
  22.             if ($key == 4) {
  23.                 switch($hitElement) {
  24.                     case 1: 
  25.                         echo 'Calling fn();' . PHP_EOL;
  26.                         break;
  27.                     case 0:
  28.                         echo 'Calling differentfn();' . PHP_EOL;
  29.                         break;
  30.                 }
  31.             }
  32.         }
  33.     }
  34. }
File Description
  • recursive
  • PHP Code
  • 06 Oct-2022
  • 984 Bytes
You can Share it: