Home >Backend Development >PHP Problem >PHP only has index arrays, right?
wrong. PHP not only supports index arrays with numbers as keys, but also supports associative arrays with strings or a mixture of strings and numbers as keys. The subscript (key name) of the index array consists of numbers, starting from 0 by default. Each number corresponds to the position of an array element in the array, and does not need to be specified. The subscripts (key names) of associative arrays are composed of a mixture of numeric values and strings. If a key name in an array is not a number, then the array is an associative array.
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
is incorrect. There are not only index arrays in php, but also associative arrays.
Array An array is an ordered set of variables in which each value is called an element. Each element is distinguished by a special identifier called a key (also called a subscript).
Each entity in the array contains two items, namely key and value. The corresponding array elements can be obtained by key value. These keys can be numeric keys or association keys. If a variable is a container that stores a single value, then an array is a container that stores multiple values.
In a PHP array, no matter what type of key name there is, there will be a value corresponding to it, that is, a key/value pair. Depending on the data type of the array key name, We can divide PHP arrays into two types:
those with numbers as key names are called indexed arrays (Indexed Array);
An array in which strings or strings and numbers are mixed as keys is called an Associative Array.
1) Index array
The subscript (key name) of the index array consists of numbers, starting from 0 by default, and each number corresponds to The position of an array element in the array does not need to be specified. PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value.
<?php $arr = array('华为','三星','vivo','oppo'); print_r($arr); echo '<br/><br/><br/>'; echo '数组 $arr 中的,键名为2的键值为:'.$arr[2]; ?>
can also be written like this:
<?php $arr1 = array('0' => '苹果','1' => '香蕉','2' => '橘子','3' => '李子','4' => '草莓'); print_r($arr1); echo '<br/><br/>'; $arr2[0] = '苹果'; $arr2[1] = '香蕉'; $arr2[2] = '橘子'; $arr2[3] = '李子'; $arr2[4] = '草莓'; print_r($arr2); ?>
2) Associative array
The subscript (key name) of an associative array is composed of a mixture of numeric values and strings. If a key name in an array is not a number, then the array is an associative array.
The key name of the associative array can be any integer or string. If the key name is a string, add a delimiting modifier to the key name - single quotes ''
or double quotes ""
. For indexed arrays, in order to avoid confusion, it is best to add delimiters.
<?php $arr1 = array('Apple' => '苹果','Banana' => '香蕉','Orange' => '橘子','Plum' => '李子','Strawberry' => '草莓'); print_r($arr1); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of PHP only has index arrays, right?. For more information, please follow other related articles on the PHP Chinese website!