The best PHP array Tutorial In 2024, In this tutorial you can learn Examples,What arrays are?,Create an array in PHP,PHP numeric array,Examples,Gets an array of length - count () function,Examples,Numeric array traversal,Examples,PHP associative array,Examples,Traverse an associative array,Examples,Multidimensional Arrays,Complete PHP Array Reference,
Array can store multiple values in a single variable:
An array is a special variable multiple values can be stored in a single variable.
If you have a list of items (for example: a list of car names), which is stored in a single variable, as follows:
However, if you want to loop through the array and find a specific one? If the item array of more than three but 300 it?
The solution is to create an array!
Arrays can store multiple values in a single variable, and you can access the value based on the key.
In PHP, array () function is used to create an array:
In PHP, there are three types of arrays:
There are two ways to create an array of numerical methods:
Automatically assigned ID key (key ID always starts at 0):
Artificial assigned ID keys:
The following example creates a numeric array called $ cars and to assign an array of three elements, then print some text that contains an array of values:
count () function returns the length of the array (number of elements):
Through and print all numeric values in the array, you can use a for loop, as follows:
Associative array is to use an array of the specified key you assigned to the array.
There are two ways to create associative arrays:
or:
You can then use the key specified in the script:
Through and print all the values in an associative array, you can use the foreach loop, as follows:
Multidimensional arrays will be described in detail in the Advanced PHP tutorial section.
For a complete reference manual for all array functions, visit our PHP Array Reference Manual .
This reference manual provides a brief description of each function and application examples!