PHP Arrays are collections of values stored in a single variable. Arrays are a powerful feature for developers because they can store and transform data in a variety of ways.
Here we’ll cover PHP arrays, including how to create and initialize arrays, how to get and modify array items, and how to work with multidimensional arrays. You’ll have a clear understanding of PHP arrays by the end of this article.
The following is a basic example of the array() function:
What is an Array?
An array is a collection of elements of the same datatype or of different datatypes.
Arrays allow you to store and manipulate a collection of data items under one variable name.
It is a special variable that can hold multiple values.
The following is an example of storing a list of items (a list of flower names, for example) in single variables:
$flowers1 = “Sunflower”; $flowers2 = “Tulip”; $flowers3 = “Rose”;
How would you go about looping through all of the flowers to find a specific flower?
Suppose you were to have 300 flowers instead of three, what would you do?
As you can see, arrays can hold many values under a single name. If you refer to the index number of the array, you can access the values in that array.
Create a PHP Array
In PHP Arrays are created through array() function:
You can create arrays with or without parameters by using the array() function.
The following is the basic syntax for creating an empty array with array() :
$array_name = array();
As shown in the example, array() creates an empty array and assigns it to $array_name.
With the array() function, you can also create an array with initial values. Here is the syntax:
$array_name = array(value1, value2, value3, ...);
In above example, by using array() an array will be created with the specified values and will be assigned to the variable $array_name.
There are three types of array in PHP:
|Indexed arrays||With numerical indices.|
|Associative arrays||Key-value arrays.|
|Multidimensional arrays||arrays containing multiple arrays.|
PHP Array Length Through count() Function
The count() function counts the elements in an array or the properties in an object.
As a parameter, it takes an array or object and returns the number of elements.
count($array_or_object, $mode = COUNT_NORMAL);
|$array_or_object||An array or object whose elements or properties you would like to count.
The parameter is required.
|$mode||This parameter is optional and specifies the mode of counting. A COUNT_NORMAL value is used by default.
The function will count recursively if you set it to COUNT_RECURSIVE, meaning that it will count every element in a multidimensional array.
When set to COUNT_NORMAL, the function returns only the elements at the top of an array or object.
In below example, the count() function counts the value of the array flowers:
The $colors array is a two-dimensional array that contains color names as keys, and each color key has an associated array that contains different shades of the color as values.
The first echo statement uses the count function to get the number of elements in the $colors array.
Since the $colors array is a two-dimensional array, this will return the number of top-level elements in the array, which is the number of color keys. This will output “Normal count: 3” since there are 3 color keys in the array.
The second echo statement uses the count function with a second argument of 1, which tells the function to count all the elements in the array recursively.
This means that it will include counting not only the top-level elements (i.e., the color keys), but also all the nested arrays and their elements. This will output “Recursive count: 10” since there are a total of 10 elements in the array when calculated recursively.
The second argument of 1 is optional and only needs to be included if you want to count the array elements recursively.