PHP: Printing Associative Array

Tu Hoang picture Tu Hoang · Jun 21, 2011 · Viewed 62.9k times · Source

In PHP, I have an associative array like this

$a = array('who' => 'one', 'are' => 'two', 'you' => 'three');

How to write a foreach loop that goes through the array and access the array key and value so that I can manipulate them (in other words, I would be able to get who and one assigned to two variables $key and $value?

Answer

Thiago Silveira picture Thiago Silveira · Jun 21, 2011
foreach ($array as $key => $value) {
    echo "Key: $key; Value: $value\n";
}