Arrays, also known as vectors, are widely used in programming to solve logical problems, often related to processing information in queue, stack or list.
An array consists of different positions that may be accessed via the respective command, which indicates the position by name or number.
By definition, the array is nothing more than a mathematical data structure, a pattern where the values can be stored in rows and columns. This way you can organize the data and use them later.
CREATING ARRAYS IN PHP
The creation of an array in PHP can be done in two ways. In the first example we have an array being created using the array () function without the indication of the constructor:
<? Php
Vet $ = array ();
$ Vet [‘Name’] = “Luiza”;
$ Vet [‘Phone’] = “11-33224555”;
$ Vet [‘Balance’] = 90,000;
?>
Note that each position of the array is determined by a name that describes the stored value and the stored value that is assigned to the respective position.
This structure is widely used in functions that return database queries. Each position of the array is indicated with the name of the column.
In the next example we have a different situation:
<?
$ Ar1 = array (‘value1’, ‘value2’, “value3”, “value x ‘);
print_r ($ ar1);?>
To use the function builder, each position receiveS Na indicator number and the stored value will be then rescued as follows:
ar1 [0] = value1
ar1 [1] = value2
ar1 [2] = value3
ar1 [3] = value x
The first position of the array will always be the zero position. Therefore, when one rescues the information you need to consider this item.
The use of the repetition structure is often used when it is necessary to go through an array in order to manipulate their values. In the following example, let’s go a four-position array, printing on screen the value of each position.
<? Php
$ Ar1 = array (‘value1’, ‘value2’, “value3”, “value x ‘);
for ($ var = 0; $ var <4; ++ $ var)
{
echo ($ ar1 [$ var] “.”);
}
?>
The zero position until the three position, all values will be printed on screen sequentially. It is important to be alert to stop parameter since, when accessing a non-existent position, the system will return an error advising that could not perform the action and the function will no longer run.
Check out more content on our blog!
Learn all about Scriptcase.
You might also like…