News Tutorials Projects Reviews Authors Contact Search Links Admin
 

Array Basics : Your Basic Array

It may be helpful to think of an array as a container that holds orderd sets of information. The infromation is stored in the form of "Key = Value." For instance:


Let's look at the following data set.

	0 	=	apple
	1	=	orange
	2	=	bananna
	3	=	grape


* Notice that we started at 0 and not 1. This is because array keys
start counting at 0.

Now let's put them into an array called "fruits":

	$fruits = array("apple", "orange", "bananna", "grape");

... and we get the following:

	$fruits[0] == "apple"
	$fruits[1] == "orange"
	$fruits[2] == "bananna"
	$fruits[3] == "grame"

You can also assign names to the keys, rather than using
the default numbers (see below) but we won't be using this
feature in our script.

	$fruits = array("zero" => "apple",
		     "one" => "orange",
		     "two" => "bananna",
		     "three" => "grape");

Which yields:

	$fruits["zero"] == "apple"
	$fruits["one"] == "orange"
	$fruits["two"] == "bananna"
	$fruits["three"] == "grame


You can also nest arrays inside on another, and creat multi-dimensional arrays. The same basic concepts apply to those, as well. You simply have a large container, filled with smaller containers, which hold your data.

 


Author: Kelli
Last edited on: 8th Dec, 12:46 am
Please rate this article.

tutorial Pages