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.

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:

Php array
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 . , , , . , , . , – , .

PHP associative array




, . .

Php associative array




, .




All Articles