Quick Reach
What is an array
An array is a type of variable in PHP that can store multiple values.
See an example of a numeric array
- 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.
Experience this example online
1
2
3
4
5
|
<?php
//Simple variable
$state = “New York”;
print $state;
?>
|
If you run the code it will simply display:
New York
Now let us do it by using a PHP Array
Experience this example online
1
2
3
4
5
6
7
8
9
|
<?php
// States in an array
$states = array (‘Alabama’, ‘Arkansas’, ‘California’, ‘Los Angles’, ‘Florida’, ‘New York’);
print_r ($states);
?>
|
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);
$arrname[0]=”array value”;
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.
See example:
Experience this example online
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
27
28
29
|
<?php
//Define an array
$emp_array[0] = “Mike”;
$emp_array[1] = “Sally”;
$emp_array[2] = “Charlie”;
$emp_array[3] = “Clare”;
// Print array in php program using echo statement
echo $emp_array[0];
echo $emp_array[1] . “</BR>”;
echo “employee number 3 is:” . $emp_array[2] . “</BR>”;
// Print array by using print statement
print $emp_array[0];
print $emp_array[1] . “</BR>”;
print “employee number 3 is:” . $emp_array[2];
?>
|
In the above example, you can see an array is created by using element numbers that starts from 0 i.e. $emp_array[0], $emp_array[1] 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:
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?php
$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
Leave A Comment?