How does a PHP array work?

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.

  1. Simple - in it, each element is set by an index in a certain sequence.
  2. 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:

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

  1. Itโ€™s easy to work simultaneously with many array values. It is 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 items.
  3. There are many different functions in PHP that allow you to process arrays. There is a search for specific values, sorting, combining arrays.

Kinds

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:

Php array
The keys used in the example are the numbers in brackets [], and the values โ€‹โ€‹are the names of fruits and vegetables.

Assigning an array of values โ€‹โ€‹to a PHP element can be written as follows:

  • $ array [n] = z;
  • n is the key, z is the value.

In the second method of initialization, you can specify nothing in square brackets :

  • $ name [] = "one";
  • $ name [] = "two";
  • $ name [] = "three".

In this case, the indices will be by default equal to: 0, 1 and 2.

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 of 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 a PHP associative array is. An index can be a string, no restrictions are imposed on it, 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 indices are strings are called associative.

PHP associative array

In one-dimensional associative arrays contains only one key, it corresponds to a specific index. The picture above shows an example of one-dimensional and multi-dimensional associative arrays.

Php associative array

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

Source: https://habr.com/ru/post/K309/


All Articles