foreach

Iterates through each value in an array.

 

This version simply returns the values.

 

<?php

	$myArray = array("Lorenzo", "Hayden", "Pedrosa") ;

	echo "Using foreach to iterate through an Indexed array<br>" ;

	foreach ($myArray as $myValue) {

		echo "\$myArray contains $myValue<br>" ;
	}
?>

 

This version returns the keys and values.

 

<?php

	$myArray = array('Yamaha' => "Lorenzo", 'Ducati' => "Hayden", 'Honda' => "Pedrosa") ;

	foreach ($myArray as $myKey => $myValue) {

		echo "$myValue rides for $myKey<br>" ;
	}
?>

Leave a Reply