ComputersProgramming

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.

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. These 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 multi-dimensional 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 the 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:

In the example, the keys are the numbers in the brackets [], and the values are the names of the 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 indexes:

  • $ 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 a PHP associative array is. The index can be a string, it is not constrained, spaces are allowed, its length is 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 classic way, but this is not very convenient.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.delachieve.com. Theme powered by WordPress.