Learn about arrays in PHP

Tram Ho

Arrays

What Is Array?

Array is a data type that allows us to store many values ​​in it including storing the value of one or more other arrays.

Arrays In PHP

Arrays in PHP are divided into 3 different types:

  • Indexed array
  • Combiative array (associative array)
  • Multidimensional array All 3 types above have the same syntax when creating arrays.

Array Syntax

To create arrays in PHP we can use the syntax:

With PHP versions 5.4 and later we can use the more concise syntax as follows:

For example, if you have a list of items (for example, a list of car names), storing the cars in single variables might look like this:

However, what if you want to go around cars and find a specific one? And if you don’t have 3 cars, but 300?

The solution is to create an array!

An array can contain multiple values ​​under a single name, and you can access the values ​​by referencing an ordinal number. So for example, the syntax for creating an array is as follows:

Or

Counts The Number Of Elements In The Array

To count the number of elements in a PHP array we use the count () function. The count () function is used to return the length (number of elements) of an array: Example:

The count value is 3

Indexed Array

The indexed array in PHP is the type of array where the elements are numbered as natural numbers: The following array is a numbered array with keys that are consecutive natural numbers. head from 0:

or an ordinal numbered array can be specified manually:

To loop through the elements in the numbered array we often use for loop:

Associative Array

The associative array (associative array) in PHP is an array using the key as an arbitrary value instead of consecutive natural numbers. There are two ways to create an associative array:

Or

For example

The result: Peter is 35 years old.

To loop through each element in the associative array we often use foreach:

Result:

In case you don’t need the key but only the value of the element:

Multidimensional Array

A multidimensional array is the type of array that contains another array inside it: PHP supports multidimensional arrays with depths of two, three, four, five or more. However, it is difficult for most people to manage the array three levels deeper.

Example: We have the following table:

NameStockSold
Volvo227
BMWtentwelfth
Saab528
Land Rover114

We can store data from the above table in a two dimensional array. The two-dimensional $ cars array contains four arrays and it has two indices: rows and columns. To have access to the elements of the array $ cars, we must point to two indices (rows and columns):

We can also put a for loop inside another for loop to get the elements of the array $ cars (we still have to point to two indices):

Array sorting functions

If you need to reset the order of values ​​inside the array, the following functions can be used:

1. sort () – Arrange the array in ascending order.

Example: Sort the elements of the array $ car in ascending alphabetical order:

2. rsort () – Put the array in descending order.

Example: Sort the elements of the array number in descending numerical order:

3.asort () – Arrange the array in ascending order, based on value.

Example: Sort an associative array in ascending order, by value:

4. ksort () – Sort arrays in ascending order, based on key.

Example: Sort an associative array in ascending order, by key:

5. arsort () – Sort arrays in descending order, based on value.

Example: Sort an associative array in descending order, by value:

6.krsort () – Sort array in descending order, based on key.

Example: Sort an associative array in descending order, by key:

References:

Share the news now

Source : Viblo