Earlier we looked at how variables can store a single value, such as a single number or a single string. But in addition, we can store a set of values in a variable. And that’s what arrays are for.
There are several ways to define arrays. The first way is using the array() function :
$numbers = array();
In this case, an empty array is defined as $numbers.
The second way is to use square brackets [] :
$numbers = [];
When defining an array, we can immediately pass the initial data to it. If square brackets are used, then the array elements are passed inside the brackets:
$numbers = [1, 2, 3, 4];
A similar definition of an array using a function array():
$numbers = array(1, 2, 3, 4);
Objects of any type can act as array elements.
Keys are used to access array elements. The key can represent a number or a string, or both numbers and strings.
For regular arrays, the key represents a number. Numeric keys are also called indices. Index numbering starts from zero, that is, the first element of the array has an index of 0, the second element has an index of 1, and so on.
For example, let’s look at the third element of the array:
four <?php $numbers = [1, 4, 9, 16]; echo $numbers[2]; // 9 ?>
Since the index numbering starts from zero, to refer to the third element, you need to use index 2.
Thus, we get the value of an array element. But we can also change it:
<?php $numbers = [1, 4, 9, 16]; $numbers[1] = 6; echo $numbers[1]; // 6 ?>
In this case, the number of array elements must be taken into account. So, we cannot refer to an element with a non-existent index:
four <?php $numbers = [1, 4, 9, 16]; echo $numbers[4]; // 6 ?>
In this case, there are only 4 elements in the $numbers array, so the index of the last element will be 3. Thus, the element with index 4 does not exist in the array, and if we try to get its value, echo $numbers[4]PHP will show us a warning.
However, if we want to set an element at an index that doesn’t exist yet, we can do this:
<?php $numbers = [1, 4, 9, 16]; $numbers[5] = 76; echo $numbers[5]; // 76 ?>
This sets the element at index 5. Once set, we can get its value. However, the element with index 4 still does not exist.
To add a new element to the array, we can, as in the example above, simply set the new element at an index that has not yet been set. But there is another way:
<?php $numbers = [1, 4, 9, 16]; $numbers[] = 25; echo $numbers[4]; // 25 ?>
With this method, a new element is added to the end of the array, so here, to get a new element, you must use index 4.
To get a complete visual representation of how the keys and values of elements are mapped in a particular array, you can use the function print_r, which takes an array as a parameter:
<?php $numbers = [1, 4, 9, 16]; $numbers[] = 25; print_r($numbers); ?>
Script output:
Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 [4] => 25 )
It should also be noted that it is not necessary to somehow specifically initialize the array variable – we can add elements to the array as we go:
<?php $numbers[] = 20; $numbers[] = 120; $numbers[] = 720; print_r($numbers); // Array ( [0] => 20 [1] => 120 [2] => 720 ) ?>
Operator =>
The => operator allows you to map a key to a specific value. Although we did not need such an operator when defining an array above, we can still use it. For example, the following array:
$numbers = [1, 4, 9, 16];
Will be similar to the following array:
$numbers = [0=>1, 1=>4, 2=>9, 3=>16]; // $numbers = array(0=>1, 1=>4, 2=>9, 3=>16);
Each element is defined in the following format: key => value
In the future, we can also access the elements of this array.
This operator may be needed if we want to redefine the index order. So, by default, the index numbering starts from zero and each next element has the index of the previous element + 1. The => operator, on the other hand, allows you to define your indexes manually for elements, not necessarily from zero and not necessarily in order:
$numbers = [1=> 1, 2=> 4, 5=> 25, 4=> 16]; echo $numbers[2]; // 4
We can also set the index for only one element, then for subsequent elements, the index will automatically increase by one:
$numbers = [4=> 16, 25, 36, 49, 64]; print_r($numbers);
In this case, indexing starts with the number 4 – this is the index of element 16. Then for element 25, the index will be the number 5, and so on.
Result:
Array ( [4] => 16 [5] => 25 [6] => 36 [7] => 49 [8] => 64 )
Iterating over an array
To iterate over an array, we can use the standard for method :
<?php $users = ["Tom", "Sam", "Bob", "Alice"]; $num = count($users); for($i=0; $i < $num; $i++) { echo "$users[$i] <br />"; } ?>
In this case, we do not manually determine the indexes of the elements, so the indexing starts from zero and increases by one for the next element. Therefore, we can $ipass the index through the variable in a loop, starting from zero. The only difficulty may be how to determine the index of the final element in order to set the ceiling for the variable $i. In this case, we can use the built-in function count() to get the length of the array. This function takes an array as a parameter and returns its length.
The result of the script:
Tom
Sam
Bob
Alice
foreach loop
Nevertheless, the enumeration method used above will not help if the indices are determined manually and they differ from neighboring indices not by one, but by an arbitrary value. In this case, we can use a special loop – foreach :
<?php $users = [1 => "Tom", 4 => "Sam", 5 => "Bob", 21 => "Alice"]; $num = count($users); foreach($users as $element) { echo "$element<br />"; } ?>
In the foreach loop , all elements are retrieved from the array one by one, and their value is placed in the variable specified after the as keyword . $elementIn this case, all four values from the array are placed into the variable in turn $users. When the last element from the array is retrieved, the loop ends.
As a result, we will get the same result:
Tom
Sam
Bob
Alice
The foreach loop allows you to extract not only values, but also element keys:
</pre> <!--?php $users = [1 => "Tom", 4 => "Sam", 5 => "Bob", 21 => "Alice"];<br ?--> $num = count($users); foreach($users as $key => $value) { echo "$key - $value "; } ?> <pre>
Here, when iterating over the elements of the cycle, the $keykey of the element will be transferred to the variable, and $valueits value will be transferred to the variable.
The result of the script:
1-Tom
4-Sam
5 – Bob
21- Alice