What is an array
An array is a type of variable in PHP that can store multiple values.
- PHP array is a data structure that can hold one or more values in a single variable.
- The Arrays are basically mapped where each key is mapped to a value.
- The array index of PHP starts from 0.
- You can create array elements in two different ways which are explained below.
Why using PHP Array
For the beginners who have no idea about arrays may ask why we use arrays? Let us take an example to understand why arrays are utilized to store the values.
Consider, we want to store 50 US States in our PHP program in string variables. One way is to define 50 string variables where each variable can store one State name. For 50 State names, fifty variables will be defined and each will be assigned a State name.
The other way is to use arrays where you simply make the $State variable an array. By using that array, you can store all 50 names in that single array. Is not that simple?
How to make that $State an array is shown below.
An Array PHP Example
In this example, we declared and assigned a simple string variable, $state and assigned it a value. After that, we displayed the value by print statement.
$state = “New York”;
If you run the code it will simply display:
Now let us do it by using a PHP Array
// States in an array
$states = array (‘Alabama’, ‘Arkansas’, ‘California’, ‘Los Angles’, ‘Florida’, ‘New York’);
Similarly, if you need to store 100 numbers then you have to define 100 simple variables. On the other hand, it is quite easier and simpler to create an array of 100 elements.
How to Create or define a new Array in PHP
These are the ways to define the arrays:
$arr_name = Array(1,2,3,4);
First of all, an array ($arrname) is defined by using the array name followed by Array keyword on the right side along with elements. The array is created with four elements.
In the second line, an array is defined by array name ($arrname) which is followed by the element number in braces  to assign a value. The array element is on the left side and the value at the right side.
We have used the first way in the above example. The second way of defining arrays is shown in the example below.
How to print an array
Printing an array as a whole or by elements is easy in PHP. We can use the echo or print statements to accomplish that. An array can be referred by element index value that starts from 0.
//Define an array
$emp_array = “Mike”;
$emp_array = “Sally”;
$emp_array = “Charlie”;
$emp_array = “Clare”;
// Print array in php program using echo statement
echo $emp_array . “</BR>”;
echo “employee number 3 is:” . $emp_array . “</BR>”;
// Print array by using print statement
print $emp_array . “</BR>”;
print “employee number 3 is:” . $emp_array;
In the above example, you can see an array is created by using element numbers that starts from 0 i.e. $emp_array, $emp_array and so on. To print the array element, we also used the element index in print and echo statements.
You can also loop through array elements by using the foreach loop type quite easily. See the section below.
An array example with foreach loop
Following example displays PHP array elements by using a foreach loop type of PHP. This is quite simple to use, where we simply created a numeric array of 5 elements.
After that, we used a foreach loop and displayed the array elements.
see example below:
$arr = array( 1, 2, 3, 4, 5);
foreach( $arr as $value )
echo “Value is $value <br />”;
The output will be:
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
See more details of using the foreach loop in its own chapter with more examples.
Types of arrays
Following are the array types supported in PHP. Click on any type below to go to its dedicated chapter:
See Also: array_push