PHP Arrays
Detailed look at arrays.
PHP Arrays
An array stores multiple values in one single variable:
$cars = array("Volvo", "BMW", "Toyota");
In PHP, there are three types of arrays:
- Indexed arrays - Arrays with a numeric index
- Associative arrays - Arrays with named keys
- Multidimensional arrays - Arrays containing one or more arrays
Get Array Length
The count() function is used to return the length (the number of elements) of an array.
Sorting Arrays
The elements in an array can be sorted in alphabetical or numerical order, descending or ascending.
sort()- sort arrays in ascending orderrsort()- sort arrays in descending orderasort()- sort associative arrays in ascending order, according to the valueksort()- sort associative arrays in ascending order, according to the keyarsort()- sort associative arrays in descending order, according to the valuekrsort()- sort associative arrays in descending order, according to the key