Core PHP

Foreach as Key-Value Pairs Loops

This PHP code is an example program that demonstrates how to use foreach as key-value pairs loops in PHP.

ForeachLoopsAsKeyValuePairs.php

<?php

  // Create an associative array of functions.
  $saFunctionProp = [
    "function" => "sin(x)",
    "derivative" => "cos(x)",
    "reciprocal" => "csc(x)"
 ];

  // A foreach loop to print out the description and value
  foreach($saFunctionProp as $sDescription => $sValue) {
    echo $sDescription." => ".$sValue."<br />";
  }

?>
 

Output

 
 

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