Telstra has revealed the addition of almost one million new mobile services in the six months to December 2011, but Sensis revenues plummeted 24 percent in 12 months.
read more
David M Williams
Monday, 01 October 2007 14:38
It doesn’t have to be that way. We can tell PHP to use index numbers of our choice with syntax like this:
$arr = array(1 => 10, 2 => 20, 3 => 30, 4 => 40);
In this case, $arr[1] holds the value 10 and $arr[4] holds the value 40. Actually, the index into the array doesn’t have to be consecutive. And nor does it have to be numbers. You could also declare the array like this:
$arr = array(“cat” => 10, “dog” => 20, “frog” => 30, “fish” => 40);
This time, $arr[“cat”] has the value 10 and $arr[“fish”] has the value 40. You might think this makes it harder to use a loop like the one above with a numeric counter if your array is indexed using string keys but that’s not actually the case. When we specify values like this, PHP actually considers them “keys”. The array is still indexed and we can still access any individual item by an index number but we also have a way of getting values using distinct keys which are special for each entry.
We could go on for a long time: PHP is one of the most flexible and versatile programming languages which partially owes to its popularity (the fact that it is free and open source helps a lot too!) and offers no end of options to declaring and using arrays. To illustrate, here’s some more array code. All these lines are in sequence and are acting on the same array:
$arr = array(“cat” => 10, 2 => 20, “frog” => 30.3, 55 => 40);
$arr[] = “Hello”;
$arr[“more”] = array(1, 2, 3, “a”, “b”, “c”);
The first line declares an array with four elements. The keys are not strictly numeric, nor are they strictly text strings. The key is “cat” for the first element, 2 for the second, “frog” for the third, and 55 for the fourth. You really can make any key you like, whether it is a number or a string and whether it is sequential or not – just so long as it is unique over the entire array.
You’ll also see that $arr[“frog”] holds the value 30.3 this time just to illustrate that the array doesn’t care if all its values are the same type either. The one array can hold numeric values, string values or any other data type at any given position. It doesn’t matter what type of data is stored elsewhere.
The second line adds another element to the array, with the value “Hello”. The catch this time is that there is no key (but there is an index value; there always will be.)
Finally, the third line adds another element to the array and gives it the key “more”. However, the value is not just a number or a string but another array, with six elements defined. You can print out the items in the array by using two sets of square brackets – so this line:
$echo $arr[“more”][0]
- will show the value 1, being the first item (the 0th index) in the array stored in $arr[“more”].
It might be worth thinking about all that for a moment. Arrays in PHP are hugely powerful owing to their remarkable flexibility.

|
Microsoft Office 365Try an easy-to-use set of web-enabled tools for business-class productivity services. Office 365 provides anywhere-access to email, important documents, contacts, and calendars on almost any device. |