Saturday, July 31, 2010

How to loop through a php assoative array

Looping through a php assoative array is really easy with foreach loop.

<?php

$array = array ("Fruit" => "Apple", "Animal" => "Dog", "Number" => 1, "Name" => "Jhon" );

foreach($array as $key => $value)
{
    echo $key.': '.$value;
    echo '<br />';
}

/* result

Fruit: Apple
Animal: Dog
Number: 1
Name: Jhon

*/

?>

No comments:

Post a Comment