The key name of the PHP array cannot be omitted; the characteristic of the PHP array is to map values to the type of keys. An element value must correspond to a key name. You can use the "array_values()" function to remove the specified keys in the array. Key name, returns an array containing all the values in the array, and uses numeric keys starting from 0 and increasing by 1.
The operating environment of this article: Windows 10 system, PHP version 8.1, Dell G3 computer
The key name of the PHP array cannot be omitted
The characteristic of the PHP array is to map values to the type of keys
The key of the array in PHP can be a string, and values can be of any type.
You can use the array_values() function to remove all key names in the array and retain the key values. This function can return an array containing all key values in the array.
The returned array will use numerical values. Key, starting from 0 and increasing by 1.
The syntax is:
array_values(array)
The example is as follows:
"Peter","Age"=>"41","Country"=>"USA"); print_r(array_values($a)); ?>
Output result:
Or use " array_keys($array)" statement can remove all key values and return an array containing all key names
array_keys() function returns a new array containing all key names in the array.
Grammar
array_keys(array,value,strict)
The example is as follows:
"XC90","BMW"=>"X5","Toyota"=>"Highlander"); print_r(array_keys($a)); ?>
Output result:
Recommended learning:《PHP Video tutorial》
The above is the detailed content of Can the php array key name be omitted?. For more information, please follow other related articles on the PHP Chinese website!