iterate over a dictionary in php

To iterate over a dictionary (associative array) in PHP, you can use the foreach loop.

main.php
$dict = array("key1" => "value1", "key2" => "value2", "key3" => "value3");

foreach ($dict as $key => $value) {
    echo "$key: $value<br>";
}
143 chars
6 lines

This will output:

main.php
key1: value1
key2: value2
key3: value3
39 chars
4 lines

In the foreach loop, the $key variable will contain the key of the current element, and the $value variable will contain the value. You can then use these variables to perform any required operations in the loop.

related categories

gistlibby LogSnag