In this lesson, you will learn about arrays in PHP, numeric arrays, and their usage, along with examples to better understand the topics.
An array is a variable that can retain more than one value at the moment. An array in PHP is an ordered map, a way of associating values with keys. It can be used as an array, list (vector), dictionary, collection, stack, queue, and hash table (a map implementation).
As array values can be other arrays, using trees and multidimensional arrays is also feasible. If you have a list of products (for instance, a list of fruit names), it might look like this to store the fruits in single variables:
$Fruit1 = "Apple"; $Fruit2 = "Banana"; $Fruit3 = "Mango";
But what if you want to browse all the fruits and find one in particular? What if you had 300 fruits instead of just three? The remedy is to use an array! Arrays can store multiple values under a single variable, and you can access the values by using an index number.
You can create an array using the language structure of the array()
. Any number of value pairs separated by commas must be provided as arguments. The comma is optional and can be omitted after the last array element.
The PHP index is the number starting from the zero position. The PHP array supports the storage of numbers, strings, and objects. All components of the PHP set are allocated by default to an index number. An indexed or numeric array stores a numeric index for each array element. Numeric arrays use the number as access keys, and an access key in an array variable is a reference to a memory slot. The access key is utilized when an array element is read or given a new value. Below is the PHP numeric array syntax.
There are two methods to declare an array of numbers:
1st Method:
$nameoffruits=array("value1"," value2"," value3"," value4"); // generic $nameoffruits=array("apple","banana","orange","pineapple");
2nd Method:
$ nameoffruits [n]=" apple"; $ nameoffruits [n+1]=" banana"; $ nameoffruits [n+2]=" orange"; $ nameoffruits [n+3]=" pineapple"; “$nameoffruits” is the name of the variable “[n]” is the access index number of the member "value" is the value allocated to the array member.
Example
<?php echo "PHP ***Numeric Array*** Example"; echo "<br>"; echo " ------------Ist method ------------------ "; echo "<br>"; $luxuryCars = array( "Volvo", "Audi,", "Ferrari" ); echo "I would like to have " . $luxuryCars[0] . ", " . $luxuryCars[1] . " and " . $luxuryCars[2] . "."; echo "<br>"; echo "<br>"; echo " ------------2nd method ------------------ "; echo "<br>"; $luxuryCars[0] = "Volvo"; $luxuryCars[1] = "Audi,"; $luxuryCars[2] = "Ferrari"; echo "I would like to have " . $luxuryCars[0] . ", " . $luxuryCars[1] . " and " . $luxuryCars[2] . "."; ?>
Output
PHP ***Numeric Array*** Example ------------Ist method ------------------ I would like to have Volvo, Audi, and Ferrari. ------------2nd method ------------------ I would like to have Volvo, Audi, and Ferrari
Using the foreach loop, we can readily cross an array in PHP. Let’s see a simple example to go through all the PHP array components.
Example
echo "PHP ***Numeric Array*** Example"; echo "<br>"; echo " ------------Traversing PHP Numeric Array ------------------ "; echo "<br>"; $luxuryCars = array( "Volvo", "Audi", "Ferrari" ); foreach ( $luxuryCars as $lc ) { echo "luxuryCars are: $lc<br />"; } ?>
Output
PHP ***Numeric Array*** Example ------------Traversing PHP Numeric Array ------------------ luxuryCars are: Volvo luxuryCars are: Audi luxuryCars are: Ferrari
PHP offers the count()
function to give the length of an array.
Example
<?php echo "PHP ***Numeric Array*** Example"; echo "<br>"; echo " ------------ PHP Count Function over Numeric Array ------------------ "; echo "<br>"; $luxuryCars = array( "Volvo", "Audi", "Ferrari" ); echo count($luxuryCars); ?>
Output
PHP ***Numeric Array*** Example ------------ PHP Count Function over Numeric Array ------------------ 3
This concludes the PHP Array lesson. In the next lesson, you will learn about Array Functions.