The keys of the php array can be numbers. In PHP, index arrays with numbers as key names are supported; 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 specifically specified. PHP will automatically assign an integer value to the key name of the index array, and then automatically increase from this value.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
The keys of the php array can be numbers .
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).
PHP arrays are more flexible than arrays in other high-level languages. Not only does support index arrays with numbers as keys, but also supports strings or a mixture of strings and numbers as keys. Associative array.
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.
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); ?>
##Expand knowledge: 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 Can keys in php arrays be numbers?. For more information, please follow other related articles on the PHP Chinese website!