The following php snippet will display both the key and the value of each element in an associative array $ar. However, couldn't it be done in a more elegant way, such that the key and the value pointers are advanced by the same mechanism? I still would like it to be executed in a foreach
loop (or some other that does not depend on knowledge of the number of elements).
<?php
$ar = array("A" => "one", "B" => "two", "C" => "three");
foreach($ar as $a){
echo "key: ".key($ar)." value: ".$a."\n";
$ignore = next($ar);//separate key advancement - undesirable
}
?>