PHP Articles
Basic & Beginners
Advanced
Database
XML, Webservices
Design Patterns
Ajax
All Articles
Main Menu
Home
About PHP Hacks
Links
Contact Us
Search


Introduction to Arrays PDF Print E-mail
Thursday, 06 July 2006

If until now you have used simple variables to store data, then there’s more to the story. Arrays can hold multiple data items, assigning each one a numeric or text index (also called a key). For example, if you want to store some student test scores, you can store them in an array, and then you can access each score in the array via a numeric index. That's great as far as computers are concerned because you can work through all the elements in an array simply by steadily incrementing that index, as you might do with a loop. In that way, you can use your computer to iterate over all the elements in an array in order to print them out or find their average value, for example.

 

You can create arrays by assigning data to them, just as you do with other variables. You can give arrays the same names as you give to standard variables, and like variable names, array names begin with a $. PHP knows you're working with an array if you include [] after the name, like this:

$students[1] = "jack";


This creates an array named $students and sets the element at index 1 to "jack". From now on, you can refer to this element as you would refer any simple variable; just include the index value to make sure that you reference the data you want, like this:

echo $students[1];


This statement echoes "jack". And you can add new values with different numeric indexes:

$students[2] = "jill";
$students[3] = "tom";


Now you can refer to $students[1] (which is "jack"), $students[2] (which is "jill"), and $students[3] (which is "tom"). In this case, we've stored strings using numeric indexes, but you can also use string indexes. Here's an example:

$students_id["Karla"] = 2343;
$students_id["Anthony"] = 5778;
$students_id["Richard"] = 18843;


You can refer to the values in this array by string, as $students_id["Karla"] (which holds 2343), $students_id["Anthony"] (which holds 5778), and $students_id["Richard"] (which holds 18843). Note that in PHP, the same array can have both numeric and text indexes, if you want to set things up that way.

There's also a shortcut for creating arrays, you can simply use [] after the array's name. Here's an example:

$students[] = "jack";
$students[] = "jill";
$students[] = "tom";


In this case, $students[0] will end up holding "jack", $students[1] will hold "jill", and $students[2] will hold "tom".

PHP starts numbering array elements with 0. If you wanted to loop over all the elements in this array, you'd start with 0, as in this for loop. Note, that you use the count function to find the number of elements in an array:

for ($index = 0; $index < count($students); $index++){
    echo $students[$index], "\n";}


Here's an even shorter shortcut for creating an array, using the PHP array function:

$students = array("jack", "jill", "tom");


This also creates the same array, starting from an index value of 0. What if you wanted to start with an index value of 1? You could specify that with => like this:

$students = array(1 => "jack", "jill", "tom");


Now the array would look like this:

$students[1] = "jack";
$students[2] = "jill";
$students[3] = "tom";


You can create arrays with text as index values in the same way:

$students_id = array("Karla" => 2343,
"Anthony" => 5778, "Richard"] => 18843);


This creates the following array:

$students_id["Karla"] = 2343;
$students_id["Anthony"] = 5778;
$students_id["Richard"] = 18843;


The => operator lets you specify key/value pairs. For example, "Karla" is the key for the first element, and 2343 is the value.

Here's another shortcut: if you have a well-defined range of data, you can automatically create array elements to match with the range function, such as the numbers 1 to 10 or characters "a" to "z" like this: $values = range("a", "z");.

Note: If you want to look inside an array, head straight for the print_r() function, which X-rays the contents of any PHP variable or structure. Try running it on any of the arrays in this tutorial, and you’ll see exactly what I mean!

PHP comes with a wide range of functions to define, create and manipulate array elements, and process them with loops. Now, that you know what an array is, start exploring functions for array’s.

Comments
Add NewSearchRSS
Only registered users can write comments!
Last Updated ( Thursday, 06 July 2006 )
 
< Prev   Next >

Syndicate


Login Form





Lost Password?
No account yet? Register