PHP arrays can use numeric key names or string key names. In PHP, it supports index arrays with numbers as keys, and 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. There is no need to specify it. PHP will automatically assign an integer value to the key name of the index array. It then automatically increments from this value.
The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer
In PHP, an array is an ordered set of Variables where 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 numerical keys or associated keys. If a variable is a container that stores a single value, then an array is a container that stores multiple values.
PHP arrays are more flexible than arrays in other high-level languages. They not only support index arrays with numbers as keys, but also support associative arrays with strings or a mixture of strings and numbers as keys.
Note: Arrays in PHP actually do not distinguish between indexes and associative arrays. The corresponding key value is found based on the key name.
1. Index array
The index array stores a series of organized single or multiple values, each of which can be Use an unsigned integer value for access.
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. There is no need to specify it. PHP will automatically set the key of the index array. Assign an integer value to the name, and then automatically increment from this value.
Index array initialization example:
<?php header("Content-type:text/html;charset=utf-8"); //创建一个索引数组,索引数组的键是“0”,值是“苹果” $fruit=array("苹果","香蕉"); var_dump($fruit); ?>
Running result:
Three assignment methods for index array:
array[0]='Apple';
$arr=array('0' =>'Apple');
##$arr=array('Apple');
<?php //请创建一个数组变量arr,并尝试创建一个索引数组,键是0,值是苹果 $arr=array(0=>'苹果'); if( isset($arr) ) {print_r($arr);} ?>
<?php $fruit=array('苹果','香蕉','菠萝'); foreach($fruit as $key=>$value){ echo '<br>第'.$key.'值是:'.$value; } ?>
第0值是:苹果 第1值是:香蕉 第2值是:菠萝
2. Associative array
"Associative array" is a Arrays with special indexing methods. Index it using a string or other type of value (except NULL). In fact, the difference between associative arrays and index arrays is only in the key value. The key value of the associative array is a string, and it is an artificial regulation, for example:<?php //创建一个关联数组,关联数组的键“orange”,值是“橘子” $fruit=array('orange'=>'橘子'); echo $fruit['orange']; ?>
Extended knowledge: Convert associative array to index array
In PHP, you can use the array_values() function to convert an associative array to an index array. array_values() function is to return the values of all elements in the array. It is very simple to use. With only one required parameter, it can return an array containing all the values in the given array, but does not retain the keys. name. The returned array will be in the form of an indexed array, with array indices starting at 0 and increasing by 1. array_values() function is particularly suitable for arrays with confusing element subscripts, or for converting associative arrays into indexed arrays.<?php header("Content-type:text/html;charset=utf-8"); $arr=array(1=>"1","a"=>"",2=>"2","b"=>0,"c"=>"blue"); echo "原关联数组:"; var_dump($arr); $res=array_values($arr); echo "转换后的数组:"; var_dump($res); ?>
PHP Video Tutorial"
The above is the detailed content of What key names can be used in php arrays. For more information, please follow other related articles on the PHP Chinese website!