How can I target keys and values for a php array in a foreach loop?

Very new to PHP: I have an array of $books, with their isbn’s as keys and names as key values:

$books["978-0743261690"] => "Gilgamesh";
$books["978-0060931957"] => "The Odyssey";
$books["978-0192840509"] => "Aesop's Fables";
$books["978-0520227040"] => "Mahabharta";
$books["978-0393320978"] => "Beowulf";

How could I use the keys alone, and their values alone, in my foreach loop? What will be the syntax?

<?php foreach($books as $book) { } ?>

http://php.net/manual/en/control-structures.foreach.php

There are two syntaxes:

foreach (array_expression as $value)
statement
foreach (array_expression as $key => $value)
statement

Chances are one of the examples there will demonstrate what you’re thinking of.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.