The only identifier in a PHP array is the key. In PHP, each element of an array is distinguished by a special identifier called a key (also called a subscript). In a PHP array, the key can be a number or a string. An array with a pure number as the key name is an index array, and an array with a string or a mixture of strings and numbers as the key name is an associative array.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In PHP, an array is a group of An ordinal variable where each value is called an element. Each element is distinguished by a special identifier called a key (also called a subscript).
Simply put: Each entity in the array contains two items, namely key and value; the key can be a number or a string.
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: index arrays and associative arrays
1. The one with pure numbers as the key name is the index array
The subscript of the index array ( The key name) consists of numbers, starting from 0 by default. Each number corresponds to the position of an array element in the array. There is no need to specify it. PHP will automatically assign an integer value to the key name of the index array, and then automatically start from this value. incrementally.
Example 1:
<?php header("Content-type:text/html;charset=utf-8"); $array[] = '香蕉'; $array[] = '苹果'; $array[] = '橘子'; $array[] = '榴莲'; //输出语句 var_dump($array); ?>
Example 2:
<?php header("Content-type:text/html;charset=utf-8"); $array= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); //输出语句 var_dump($array); ?>
2. An array in which strings or strings and numbers are mixed as key names is an associative array
The subscript (key name) of an associative array is composed of a mixture of numerical values and strings. If there is a If the key name is not a number, then the array is an associative array.
<?php header('content-type:text/html;charset=utf-8'); $arr = array('Apple' => '苹果','Banana' => '香蕉','Orange' => '橘子','Plum' => '李子','Strawberry' => '草莓'); var_dump($arr); ?>
The key name of an associative array can be any integer or string. If the key name is a string, add a delimiting modifier to the key name - single quote '' or double quote "". For indexed arrays, in order to avoid confusion, it is best to add delimiters.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the unique identifier in PHP array?. For more information, please follow other related articles on the PHP Chinese website!