How can I get the current index value of the foreach loop in php

advertisements

How should I get the current value of the array

 foreach ($key1 as $key => $value) {

                            }

Here, $key represent the current index of an array.. But how do I find the current value of the current index. Please someone help me..


In PHP, you can have associative arrays with key => value pairs.

Example:

$countryCapitals = ['Germany' => 'Berlin', 'UK' => 'London', 'Netherlands' => 'Amsterdam', 'France' => 'Paris'];

When you are iterating with a foreach loop you can access the key value pair as

foreach ($countryCapitals as $country => $capital) {
    echo 'The capital of ' . $country . ' is ' . $capital . "\n";
}

where the country is the key and the capital is the value.