php: working with arrays
As a developer you already know that a variable is a bucket in which you can temporarily store a value. Using these values from the bucket you can create a script that stores, processes, and outputs information everytime it is run. Unfortunately, you can only save one value per variable at a time. Arrays are special variables that enables you to overcome this problem. An array can store many values as you want in the same variable. Each value is indexed within the array by a number or string.
Each item in an array is referred to as an element. Each element can be accessed directly via its index. By default, array elements are indexed by numbers, starting at 0. With array you can loop through each item or pull one out at random. You can sort an array numerically, alphabetically or even the way you want it to sort.
Creating Arrays
As pointed out above that arrays are lists of values indexed by number or string. Values can be assigned to an array in two ways: with the array() construct or directly using the the array operator []. It is important that you know how to use both.
Array with array() construct
The array() construct is useful when you want to assing multiple values at an array at one time. Lets define an array called $furits and assign four strings to it.
$furits = array ("banana", "apple", "orange", "pear");
You can now access any element in the $furits array by referring to its index. example
print $furits[2]; //prints orange
Remember that arrays are indexed from zero by default, so the index of an element in an sequentially indexed array always is one less than the element’s place in the list.
Now let’s re-create our array using the array operator [].
$furits[] = "banana"; $furits[] = "apple"; $furits[] = "orange"; $furits[] = "pear";
Again you can access any element by referring to it’s index as shown above.
Adding to an array
Now lets say you wanted to add more items to your $furits array, well one way of doing it is by adding the item in the array() construct and the second way of adding an item is by using the array indentifier in conjunction with the array name. example
$furits = array ("banana", "apple", "orange", "pear", "mango");
or
$furits = array ("banana", "apple", "orange", "pear"); $furits[] = "mango";
Associative Arrays
Associative array enables you to access elements by name rather than numbers. An associative array is indexed with strings between the squared brackets. Imagine an address book. Which would be easier, indexing the “name” field as 4 or as name? Ofcourse name. Once again you can create an associative array by using the array() construct or an operator [].
$addBook = array ( "name" => "waqas", "street" => "some road" );
you can now access any of the fields of $addBook by:
print $addBook['name']; //prints waqas
creating an associative array using operator is similar to how you would create a normal array. The only difference is that you have to define the fileds within the squared brackets followed by the value.
Multidimensional Arrays
Multidimensional array is an array or arrays. It’s an array that stores an array in each of it’s elements. The fact that an array element can itself be an array enables you to create sophisticated data structures relatively easily. To access an element of multidimensional array you need to use two indices:
$array[1][2]
$addBook = array ( array ( "name" => "waqas", "street" => "some road" ), array ( "name" => "ahmad", "street" => "some st" ), ); print $addBook[1]['name']; //prints ahmad
$addBook[1] access the second array and ['name'] access the name filed of the second array.
Looping through an Array
Above I showed you how to get one element from an array, but how about if you want to display all the elements within an array? well we loop through each element of an array using the foreach statement. There are many ways you can loop through an array but for this example I will show you using foreach statement.
$furits = array ("banana", "apple", "orange", "pear"); foreach ( $furits as $val ) { print "$val<br>"; }
The value for each element in $furits array is temporarily stored as $val variable. So we can than use the $val to display all the elements. Now how about if you want to loop through an assoicative array getting it’s key and value? well it’s the same, but you have to alter the foreach statement slightly.
$addBook = array ( "name" => "waqas", "street" => "some road" ); foreach ( $addBook as $key => $val ) { print "$key: $val<br>"; }
$addBook is the array that we are looping through, $key is the variable holding each keys, $val is the variable for holding each value. We can now print both key and value using the variables. We can now combine these techniques to output our multidimensional array, again we just alter the foreach statement to get the results.
$addBook = array ( array ( "name" => "waqas", "street" => "some road" ), array ( "name" => "ahmad", "street" => "some st" ), ); foreach ( $addBook as $val ) { foreach ($val as $key => $value ) { print "$key: $value<br>"; } print "<br>"; }
In the multidimensional array we need to use two foreach statement to display the results. The first foreach is to loop through the $addBook array assinging its value to a variable called $val. The second foreach statement is than used to get the values from the first loop and assign them to a $key variable which than gets each value using the $value variable.
In the second part of this article I will take you through other cool things you can do with using php arrays.




I think this is a good guide for PHP arrays. however theres more you can do with arrays, such as sort array, merge array etc
Thanks Tony. I am in the process of writting the second part of this article, which will cover more functions about arrays.
Bless your little cotton socks, Waqi! It’s all greek to me but I appreciate you giving away your almighty knowledge
Miss ya! xx