PHP get key and value in loop


A cool little trick with foreach loops in PHP is you can get both the key and the value of associative array like so:

foreach ($array as $key => $value) {
    echo "$key=$value"; 
}

Very handy if you need to check the key of the value you are working with.


Revision #1
Created Wed, Aug 7, 2019 6:53 PM by Moodle Wiki
Updated Wed, Aug 7, 2019 6:53 PM by Moodle Wiki