Array is a collection of heterogeneous elements. There are two types of arrays in PHP. First type of array is a normal one that contains integer keys (indexes) which can be found in any typical programming languages. The second type of arrays is an associative array, where the keys are strings. Associative arrays are also known as hashes.
Array Creation
Arrays in PHP are dynamic. There is no need to specify the size of an array. A normal array can be created by using the integer index and the assignment operator as shown below:
$array1[0] = 10;
If no integer index is specified, the index will be set to 1 larger than the previous largest index value used. Consider the following example:
$array2[3] = 5;
$array2[ ] = 90;
In the above example, 90 will be stored at index location 4.
There is another way for creating an array, using the array construct, which is not a function. The data elements are passed to the array construct as shown in the below example:
$array3 = array(10, 15, 34, 56);
$array4 = array( );
In the above example array4 is an empty array which can be used later to store elements.
A traditional array with irregular indexes can be created as shown below:
$array5 = array(3 => 15, 4 =>, 37, 5 => 23);
The above code creates an array with indexes 3, 4 and 5.
An associative array which contains named keys (indexes) can be created as shown below:
$ages = array(“Ken” => 29, “John” => 30, “Steve” => 26, “Bob” => 28);
An array in PHP can be a mixture of both traditional and associative arrays.
Accessing Array Elements
Array elements can be accessed using the subscript (index or key) value which is enclosed in square brackets.
Consider a traditional array as shown below:
$array1 = array(10, 20, 30, 40);
Third element (30) in the above array can be accessed by writing $array1[2]. The index of any traditional array starts with 0.
Consider an associative array or hash as shown below:
$ages = array(“Ken” => 29, “John” => 30, “Steve” => 26, “Bob” => 28);
We can access the age (30) of John by writing $ages[‘John’].
Iterating through Arrays
The foreach loop can be used to print / access the values in a traditional or an associative array.
Consider a traditional array as shown below:
$array1 = array(10, 20, 30, 40);
The foreach loop to print the values in the above array can be written as shown below:
foreach($array1 as $val)
print("$val <br />");
Consider an associative array or hash as shown below:
$ages = array(“Ken” => 29, “John” => 30, “Steve” => 26, “Bob” => 28);
The foreach loop to print the keys and values in the above array can be written as shown below:
foreach($ages as $name => $age)
print("$name age is $age <br />");
Array Functions
The array elements can be manipulated or accessed in different ways. PHP has an extensive list of predefined functions that comes in handy while working with arrays. Some these functions are mentioned below:
A video tutorial demonstrating arrays in PHP:
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply