Technology news and Jobs arrow Information Technology News arrow Vamp the Linux LAMP through PHP part 2
Vamp the Linux LAMP through PHP part 2 E-mail
by David M Williams   
Monday, 01 October 2007
Note that the array is indexed by a variable called $counter which begins at 0. That’s because, unless you specify otherwise, PHP will make arrays starting at index 0. This means we have to stop the loop before $counter is equal to the number 4 because the fourth element is actually at index 3.

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.



 
< Next story in category   Previous story in the category >
iTWire user statistics Visitors last 30 days
694,279
Subscribers 15,210
#1 independent technology news advertise here
  •   *  
  • Search
  • AdvSeach
  • Login
  • Events
  • FreeStuff

- Advertisement -

Featured Whitepapers

Follow iTWire on Twitter

About iTWire

iTWire is all about technology news, information, jobs and community for the IT and telecommunications industry professional. Subscribe to our free ICT daily newsletter