Arrays
Arrays in PHP are pretty much different from any other implementation the author has seen in other languages. The concept of an array is simple - it's a list of items:
Accessing the array is done as a whole ($my_array) or by array item. Items are numbered from 0 at the beginning, so $my_array[0] is "bob" and $my_array[3] is "thelma"Code:$my_array = array( "bob", "fred", "barney", "thelma" );
So far, this should be familiar to many coders. You don't have to declare arrays in PHP, or their length - the size of an array will adapt to meet the contents. Array items can be strings, numbers, even other arrays:
A useful function to learn here is the print_r() function to show the structure and contents of any variable. You can use it to help you visualise how this data is arranged. You can also run PHP from the command-line if you have the php-cli program in your PHP installation package (from version 4.2.x onward). To run a script from the command line, use:Code:$my_array[4] = array("billy-anne", "billy-bob", "billy-sue");
The stops the HTTP headers from showing on the command-line, which keeps your screen tidy.Code:php-cli file.php
Try saving the following lines to a file then running it through PHP.
Now, the powerful and unique feature of PHP arrays (and also a big stumbling block for many) is that PHP arrays aren't just numbered, they're named. For good coding, you shouldn't assume that your arrays are numbered, or even numbered in order! This means that a traditional for( $i = 0; $i < count($my_array); $i++) loop won't necessarily work.Code:<?
$my_array = array( "bob", "fred", "barney", "thelma" );
$my_array[4] = array("billy-anne", "billy-bob", "billy-sue");
print_r($my_array);
>
Named arrays are assigned slightly differently, but not much - you can use this format for numbered arrays too:
Hopefully you can see from this example why named arrays are useful. Later on in the database section we show how you can retrieve rows from a database query and store each row as a named array. Identifying in your HTML code what $row[4] is can be annoying and time-consuming, because you have to go back and find the SQL statement and count to the 5th field... etc. But if you see $row["age"] you know exactly what part of the data is being shown.Code:$this_user = array( "firstname" => "Bob",
"surname" => "Smith",
"age" => 24 );
$this_user['firstname'] = "Robert";
(Un)fortunately it doesn't end there with arrays - you can treat them like a stack and push things onto, or pop them off of the top, like some programmers would with assembler or forth. PHP also lets you shift and unshift items to/from the bottom of the stack too. And because you can store arrays in arrays, you can simulate trees. If you're not a full programmer yet and this means nothing to you.. good! Less work for me :-)
Read the manual's function reference - it has a whole section of array functions that let you shuffle, reverse, sort, splice, count, merge, and filter arrays. You can subtract one array from another, or build an array of the unique values of another array.. in short, you can do a helluva lot with arrays. They're one of the most powerful features of PHP.
I'll cover some simple array functions (like looping over the contents) and ask you to play with the manual a little.
This tutorial was written by another one of my friends, if you would like to use this tutorial please send me a PM
Last edited by clookid; 01-10-2007 at 09:55 PM.
Nice tutorial man,
Exciting, now I think I'm learning php as well coz I only knew HTML,XHTML
DHTML a little, I saw another thread saying how to connect to a database, I think both are linked, (tutorials).
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks