Core PHP

Using Nested Foreach Loops on Nested Arrays

This PHP code is an example program that demonstrates how to use nested foreach loops to unravel nested arrays.

NestedForeachNestedArrays.php

<?php

  // Create an array of arrays of elements.
  $iaaArray = [
    ["Matthew", "Mark", "Luke", "John"],
    ["Father", "Son", "Holy Ghost"],
    ["Wisdom", "Understanding", "Counsel", "Fortitude",
      "Knowledge", "Piety", "Fear of the Lord"],
    ["Faith", "Hope", "Charity"],
    ["Prudence", "Justice", "Fortitude", "Temperance"]
  ];

  // Use nested foreach loops to unravel the nested arrays
  foreach ($iaaArray as $iaArray) {
    echo "<ul>";
    foreach($iaArray as $iNumber) {
      echo "<li>".$iNumber."</li>";
    }
    echo "</ul>";
  }
?>
 

Output

 
 

© 2007–2024 XoaX.net LLC. All rights reserved.