Introduction
An array is a special variable that can hold many values under a single name, and you can access the values by referring to an index number or name.
Types of Array
- Indexed or Numeric Arrays
- Associative Arrays
- Multidimensional Arrays
Indexed or Numeric Arrays
These type of arrays can be used to store any type of element, but an index is always a number. By default, the index starts at zero. These arrays can be created in two different ways.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?php // One way to create an indexed array $name_one = array("Zack", "Anthony", "Ram", "Salim", "Raghav"); // Accessing the elements directly echo "Accessing the 1st array elements directly:\n"; echo $name_one[2], "\n"; echo $name_one[0], "\n"; echo $name_one[4], "\n"; // Second way to create an indexed array $name_two[0] = "ZACK"; $name_two[1] = "ANTHONY"; $name_two[2] = "RAM"; $name_two[3] = "SALIM"; $name_two[4] = "RAGHAV"; // Accessing the elements directly echo "Accessing the 2nd array elements directly:\n"; echo $name_two[2], "\n"; echo $name_two[0], "\n"; echo $name_two[4], "\n"; ?> |
Output:
1 2 3 4 5 6 7 8 | Accessing the 1st array elements directly: Ram Zack Raghav Accessing the 2nd array elements directly: RAM ZACK RAGHAV |
Associative Arrays
These types of arrays are similar to the indexed arrays but instead of linear storage, every value can be assigned with a user-defined key of string type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php // One way to create an associative array $name_one = array("Zack"=>"Zara", "Anthony"=>"Any", "Ram"=>"Rani", "Salim"=>"Sara", "Raghav"=>"Ravina"); // Second way to create an associative array $name_two["zack"] = "zara"; $name_two["anthony"] = "any"; $name_two["ram"] = "rani"; $name_two["salim"] = "sara"; $name_two["raghav"] = "ravina"; // Accessing the elements directly echo "Accessing the elements directly:\n"; echo $name_two["zack"], "\n"; echo $name_two["salim"], "\n"; echo $name_two["anthony"], "\n"; echo $name_one["Ram"], "\n"; echo $name_one["Raghav"], "\n"; ?> |
Output:
1 2 3 4 5 6 | Accessing the elements directly: zara sara any Rani Ravina |
Multidimensional Arrays
Multi-dimensional arrays are such arrays that store another array at each index instead of a single element. In other words, we can define multi-dimensional arrays as an array of arrays. As the name suggests, every element in this array can be an array and they can also hold other sub-arrays within. Arrays or sub-arrays in multidimensional arrays can be accessed using multiple dimensions.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?php // Defining a multidimensional array $favorites = array( array( "name" => "Dave Punk", "mob" => "5689741523", ), array( "name" => "Monty Smith", "mob" => "2584369721", ), array( "name" => "John Flinch", "mob" => "9875147536", ) ); // Accessing elements echo "Dave Punk email-id is: " . $favorites[0]["email"], "\n"; echo "John Flinch mobile number is: " . $favorites[2]["mob"]; ?> |
Output:
1 2 | Dave Punk email-id is: davepunk@gmail.com John Flinch mobile number is: 9875147536 |
Array Items
Array items can be of any data type.
The most common are strings and numbers (int, float), but array items can also be objects, functions or even arrays.
You can have different data types in the same array.
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 | <?php // function example: function myFunction() { echo "This text comes from a function"; } // create array: $myArr = array("Volvo", 15, ["apples", "bananas"], myFunction); // calling the function from the array item: $myArr[3](); ?> |
Output:
1 | This text comes from a function |