echo key and value of an array without and with loop echo key and value of an array without and with loop arrays arrays

echo key and value of an array without and with loop


foreach($page as $key => $value) {  echo "$key is at $value";}

For 'without loop' version I'll just ask "why?"


Without a loop, just for the kicks of it...


You can either convert the array to a non-associative one, by doing:

$page = array_values($page);

And then acessing each element by it's zero-based index:

echo $page[0]; // 'index.html'echo $page[1]; // 'services.html'

Or you can use a slightly more complicated version:

$value = array_slice($page, 0, 1);echo key($value); // Homeecho current($value); // index.html$value = array_slice($page, 1, 1);echo key($value); // Serviceecho current($value); // services.html


If you must not use a loop (why?), you could use array_walk,

function printer($v, $k) {   echo "$k is at $v\n";}array_walk($page, "printer");

See http://www.ideone.com/aV5X6.