In PHP, you can loop through an associative array and access both the keys and values using the foreach
loop. Here's an example:
<?php
$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");
foreach ($fruits as $key => $value) {
echo "Key: " . $key . ", Value: " . $value . "<br>";
}
?>
This will output:
Key: apple, Value: red
Key: banana, Value: yellow
Key: orange, Value: orange
In the foreach
loop, the $key
variable represents the key of the current element being iterated, and the $value
variable represents the value of that element. You can access them within the loop and use them as needed.