An ordered map in PHP is called an array (contains value based on the key). It's a variable that may hold several values of the same type.
PHP Array Less Code Benefits: Multiple variables do not need to be defined.Simple to traverse: We can traverse all the elements of an array with a single loop.Sorting: The items of an array can be sorted.
A PHP index is a number that starts at 0 and goes up from there. We may store numbers, strings, and objects in the PHP array, and by default, all PHP array items are given an index number.
1st way:
2nd way:
Season are: summer, winter, spring and autumn
A data structure that holds one or more similar values in a single value is known as an array. If you wish to save 100 numbers, for example, instead of setting up 100 variables, you may announce a list of 100 lengths.
There are three types of arrays, and each array value is accessed by an ID c, also known as the array index.
An array having a number index is known as a numeric array. Linearly, values are stored and retrieved.
An array using strings as the index is called an associative array. Instead of storing element values in a strict linear index sequence, this saves them in conjunction with key values.
An array comprising one or more arrays and values is referred to as a multidimensional array.
Numbers, strings, and any other object can be stored in these arrays, but numbers represent their indexes. The array index starts at zero by default.
The example below shows how to construct and access numeric arrays. To construct an array, we utilized the array() method. The function reference explains how to use it.
To construct an array, we utilized the array() method. The function reference explains how to use it.
In terms of functionality, associative arrays are pretty similar to numeric arrays. However, they differ in terms of the index. The associative array index will be a unit of characters, allowing you to create a solid link between keys and value.
A numerically indexed array would not be ideal for storing employee wages in an array. Instead, we might use the workers' names as keys in an associative array, with their salaries as the value.
NOTE:When printing, do not put the associative array within a double quotation; otherwise, it will not return any value.
Each element in the primary array can also be an array in a multidimensional array. And each sub-array element can be an array, and so on. Multiple indexes are used to access values in the multidimensional array.
We construct a two-dimensional array in this example to record the grades of three students in three topics. Although this is an associative array, you can create a numeric array in the same way.
|