An array is a data structure that allows you to store in one place certain values that are of the same type.
Array Types
There are two types of arrays; they differ in the way they identify the constituent elements.
- Simple - in it, each element is set by an index in a certain sequence.
- Associative - it uses keys associated logically with values to access an element.
In simple terms, this is a variable in which there can be more than one value. We are interested in the PHP array.
Characteristics
Consider the PHP array in more detail:
- It can contain any number of values, and it can also be empty.
- Each of the values that the PHP array contains is called an element.
- An element stores different types of variables. It can be strings, integers, logical values.
- Access to elements is possible with the help of indexes, which are lowercase and numeric.
- The PHP array contains elements with unique indexes.
- The number of elements in an array is its length.
- Element values can also be arrays, so multidimensional arrays are created.
A distinctive feature of PHP is the ability to create an array of any complexity in a script.
Advantages:
- It’s easy to work simultaneously with many array values. It is easy to loop through its elements by changing values.
- They are easy to manipulate. Just delete, add items, read or change the values of items.
- There are many different functions in PHP that allow you to process arrays. There is a search for specific values, sorting, combining arrays.
Views
Arrays are divided into 2 types:
- one-dimensional;
- two-dimensional.
There are different ways to initialize arrays. First, consider a simple, and then an associative array of PHP.
An example of creating a simple array in PHP:
The keys used in the example are the numbers in brackets [], and the values are the names of fruits and vegetables.PHP :
- $array[n] = z;
- n – , z – .
:
- $name[] = "";
- $name[] = "";
- $name[] = "".
: 0, 1 2.
:
- $name[35] = "";
- $name[18] = "";
- $name[90] = "".
:
- $name[37] = "";
- $name[5] = "";
- $name[ ] = "".
, 38, 37 – .
:
$[1][2]... .
, PHP . , , , . , , . , – , .
, . .
, .