How does the PHP array work?
An array is a data structure that allows you to store certain values in one place that are the same type of elements.
Array Types
There are two types of arrays, they differ in the way of identifying the constituent elements.
- Simple - in it each element is given by an index in some sequence.
- Associative - it uses keys that are associated logically with values to access the element.
In simple terms, it is a variable in which there can be more than one value. We are interested in 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 contains a PHP array is called an element.
- The element stores different types of variables. It can be strings, integers, logical values.
- Access to the elements is possible with the help of indexes, which are both string and numeric.
- PHP array contains elements with unique indexes.
- The number of elements in the array is its length.
- The values of the elements 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 the script.
Pluses:
- It is not difficult to work simultaneously with multiple array values. It's easy to loop through its elements by changing values.
- They are easy to manipulate. Just delete, add items, read or change the values of the elements.
- In PHP, there are many different functions that allow you to handle arrays. There is a search for certain values, sorting, combining arrays.
Kinds
Arrays are divided into 2 more 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:
Assigning a value to a PHP array element can be written like this:
- $ array [n] = z;
- n is the key, z is the value.
With the second method of initialization, you can not specify anything in square brackets:
- $ name [] = "one";
- $ name [] = "two";
- $ name [] = "three".
In this case, the indices will be equal to 0, 1 and 2 by default.
And you can assign any of your values to the indices:
- $ name [35] = "one";
- $ name [18] = "two";
- $ name [90] = "three".
You can combine initialization methods:
- $ name [37] = "first";
- $ name [5] = "second";
- $ name [] = "third".
The third element will be assigned an index equal to 38, since 37 is the largest of the indices.
The syntax of a multidimensional array looks like this:
$ name [index1] [index2] ....
Now let's see what PHP is likeassociative array. The index can be a string, it is not constrained, spaces are allowed, its length can be different. Associative arrays are good to use when you need to associate elements not with numbers, but with words. Arrays whose indexes are strings are called associative.
One-dimensional associative arrays contain only one key, it corresponds to a specific index. The example above shows an example of one-dimensional and multidimensional associative arrays.
You can create a multidimensional associative array in a classical way, but this is not very convenient.