PHP tutorial - Creating and looping array in PHP


Creating And Looping Array In PHP

What is an array? You have to understand what is it before start using it. An array is a variable which allows you to store multiple value within it! Here is an illustration. Have you ever saw a file cabinet usually found in offices, a cabinet where there's a lot of drawers to store important files for easy searching and organizing? That is what array similar to.

In PHP, it is possible to store an array within an array. PHP has the flexibility to array handling! If using the above example to illustrate it, then it would be storing a file cabinet within a file cabinet. How's that sound? Hmm ... Unfortunately, it is impossible :)

Well, to start using an array, the first step is to create an array variable.

<?php

$color = array();

?>

When PHP read this code, it will assign this variable $color as an array. But it is empty. It still has no elements within it. Now we are going to place some highly confidential files into this cabinet.

<?php

$color = array("red", "blue", "yellow");

?>

Take a look at how those elements are stored within it. First, you have to use double quote for opening and closing of a string and then followed by a comma which tells PHP to seperate each element from the others.

How to identify those array?
When you assign values into array elements, PHP uniquely identify皜each and every elements with keys. It always start with 0. As with the code above, key[0] has the value of 'red', key[1] is 'blue' and the last one key[2] is 'yellow'.

Thus, when you want to access an element of an array, you have to enter the array variable $color followed by the key number. Let say you want to print out those values stored inside. It can be done like this:

<?php

$color = array("red", "blue", "yellow");
echo $color[0];
echo $color[1];
echo $color[2];

?>

Array looping
If you had tons of elements stored within an array, it will takes you quite some time to type in one by one. Instead of typing it, you can use 'foreach' statement to loop all the value within an array.

<?php

$color = array("red", "blue", "yellow");
foreach($color as $key => $value) {
echo $key . " " . $value . "<br>";
}

?>

And the result will be

0 red
1 blue
2 yellow

Try this Try to create an array which store the days of a week. And when the script is executed, your script should find what day is today and then match it with those values stored inside the array. If the value match, your script should be able to print out the index number which represents unique key.

It is pretty challenging but is not very hard to code if you had understand how to loop, how to use if( ) statement and how to create array. Here is the additional function you may need.
**To find what day is today, use date("l") lowercase of 'L'

  

Optimize Website - Webmasters optimize their site and gain higher search engine ranking

Webmaster tools - Free online webmaster toolkits, website reporting and free PHP opensource scripts.

Free Photo Hosting - Free File Hosting, Free ZIP file Hosting, Free Image Hosting, Free MP3 Hosting.

Funny Videos, Stupid Videos

Stupid Videos - Custom Pimped Motorcycle Pictures, Videos, and Articles

Planet Diaz - Programming Community - Online programming resource for programmers all around the world

Forectory - Webmaster Directory

St Louis Computer Networking

Website Templates - 1000s of Templates $5 - Many Templates to help you build your site!

 


Each and every tutorial published in PHPhelps.com is originally written by PHPHELPS OWNERS. None of the content is allowed to be replicated without prior notice to writter.
Copyright © 2006 PHPhelps.com