/ / How does the PHP array work?

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.

  1. Simple - in it each element is given by an index in some sequence.
  2. 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:

  1. It can contain any number of values, and it can also be empty.
  2. Each of the values ​​that contains a PHP array is called an element.
  3. The element stores different types of variables. It can be strings, integers, logical values.
  4. Access to the elements is possible with the help of indexes, which are both string and numeric.
  5. PHP array contains elements with unique indexes.
  6. The number of elements in the array is its length.
  7. 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:

  1. It is not difficult to work simultaneously with multiple array values. It's easy to loop through its elements by changing values.
  2. They are easy to manipulate. Just delete, add items, read or change the values ​​of the elements.
  3. 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:

PHP array
In the example, the keys are the numbers in parentheses [], and the values ​​are the names of fruits and vegetables.

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.

Associative PHP array

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.

PHP associative array

You can create a multidimensional associative array in a classical way, but this is not very convenient.

Read more: