How to PHP Scripting- PHP Arrays
PHP Arrays are variables that stores multiple or a group of values under a single variable name. Arrays can be used in many ways to store and organize data quickly and efficiently. It is one of the more useful data types available to any programming language. Arrays can most easily be described as an ordered list of elements. You can access the individual elements by referring to their index position within the array.
There are three different types of arrays in PHP.
3. Multidimensional Arrays.
1.Indexed PHP Arrays.
A PHP array with a numeric index is commonly called an indexed array. There are two ways to create indexed arrays. Index of elements can be assigned automatically.
$seasons = array( "Autumn", "Winter", "Spring", "Summer" )
Or do it manually.
$cars = "Autumn"; $cars = "Winter"; $cars = "Spring"; $cars = "Summer";
2.Associated PHP Arrays.
An array with named positions is called an associative array. In an associative array, the keys assigned to values can be arbitrary and user defined strings.
<?php $first_array = array("key1" => "the first element", "key2" => "the second element"); $second_array = array( "key3" => "this is the first element of the second array", "key4" => "this is the second element of the second array", ); echo $first_array['key1']; echo $second_array['key3']; echo $first_array['key2']; echo $second_array['key4']; ?>
the first element. the first element of the second array the second element this is the second element of the second array
3. Multidimensional PHP Arrays.
A multidimensional array is an array that contains at least one other array as the value of one of the indexes.
<?php $contacts = array( array( "name" => "Anwar", "email" => "firstname.lastname@example.org", ), array( "name" => "Habeeb", "email" => "email@example.com", ), array( "name" => "Muhsin", "email" => "firstname.lastname@example.org", ) ); echo "Anwar's Email-id is: " . $contacts["email"]; //prints email@example.com ?>