www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons PHP Array Types Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
We can classify PHP arrays in three different types:

1. Numeric Arrays
2. Associative Arrays
3. Multidimensional Arrays

1. Numeric Arrays
A numeric array stores each element with a numeric ID key. The arrays we saw in the last lesson, were examples of numeric arrays. As we learned in the last lesson, there are two ways to create a numeric array, as shown in the following examples:

Example 1
$days = array("Friday", "Saturday", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday");
//PHP automatically provides numeric ID keys

Example 2

$days[0] = "Friday";
$days[1] = "Saturday";
$days[2] = "Sunday";
$days[3] = "Monday";
$days[4] = "Tuesday";
$days[5] = "Wednesday";
$days[6] = "Thursday";
//programmer provides numeric keys

Using the above arrays is very simple. We use $variableName[index] notation to access array elements. For example to output the sixth element of the $days array above:

echo $days[5];

2. Associative Arrays
In an associative array, each ID key is associated with a value. An index is always numeric, while a key could be a string too. An associative array stores a key, value pair. Internally PHP stores two arrays, one containing the keys, and the other containing values. PHP associates these arrays as key, value pairs, hence the name associative arrays. Like a numeric array, we can create an associative array in two ways, as shown in the following examples:

Example 1
$acronyms = array("PHP"=>"PHP: Hypertext Preprocessor", "SQL"=>"Standard Query Language", "HTML"=>"HyperText Markup Language");

echo $acronyms["PHP"]; // prints PHP: Hypertext Preprocessor

The above exmple declares an associative array. Notice the => sign. On the left side of the => sign we provide the key and on the right side of the => is the value. When accessing the array we use the notation [Key].

Example 2
$acronyms['PHP'] = "PHP: Hypertext Preprocessor";
$acronyms['SQL'] = "Standard Query Language";
$acronyms['HTML'] = "HyperText Markup Language";
echo $acronyms["SQL"]; // prints Standard Query Language

3. Multidimensional Arrays
A multidimensional array can be defined as an array of arrays. In a multidimensional array, each element in the main array can be an array, and each element in the sub-array can also be an array, and so on. In the following example we create a multidimensional array:
$countryCities = array
"Saudi Arabia"=>array
Accessing elements of a multidimensional array is very simple. Consider the following code:

echo $countryCities['Saudi Arabia'][0]; // outputs Makkah

Notice the use of [][] above. $countryCities is a two dimensional array, therfore, we used [] twice. To access the elements of a three dimensional array we need to use [] thrice and so one. Be careful while using multidimensional arrays, as they would add complications to your code. We can also print the whole array with the echo command:

echo $countryCities;

The output would be:
[Saudi Arabia] => Array
[0] => Makkah
[1] => Madina
[UAE] => Array
[0] => Dubai
[1] => Sharjah
[Pakistan] => Array
[0] => Lahore
[1] => Karachi

Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.