PHP HTML CSS Tutorials

Tutorials, Resources and Snippets

Archive for the ‘Arrays’ Category

PHP Arrays

leave a comment

Iterating through Arrays:

$items = array('PHP', 'JavaScript','AJAX', 'Python','ASP', 'C#');

//iterate using foreach
foreach($items as $val) 
{ 
     echo "$val";
}

// iterate using for loop 
for($i = 0; $i < count($items); $i++)
{
     print("$items[$i]\n");
}

Iterating through Associative Arrays:

$associative_array 
    = array("a" => "Pizza", "b" => "Burgers", "c" => "Chips");

foreach ($associative_array as $key => $val) {
    print "$key = $val\n";
}

while (list($key, $val) = each($associative_array)) {
    print "$key is $val\n";
}

Written by admin

January 12th, 2009 at 7:40 am

Posted in Arrays