Home >Backend Development >PHP Problem >How to change array key name to number in php
In PHP, you can use the array_values() function to change the array key name to a number. This function can get the values of all elements in the array and convert the associative array into an index array. Then the key name of the array It becomes a number; the syntax is "array_values(array)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In php, you can use array_values () function to change the array key name to a number.
array_values() function returns an array containing all the values in the array.
Grammar format:
array_values(array)
Parameters | Description |
---|---|
array | Required. Specifies an array. |
array_values() function is particularly suitable for arrays with confusing element subscripts, or for converting associative arrays into indexed arrays.
Return value: Returns an array containing all the values in the array. The returned array will use numeric keys, starting at 0 and increasing by 1.
Example:
<?php header("Content-type:text/html;charset=utf-8"); $array = array("颜色1" => '红色', "颜色2" => '黄色', "颜色3" => '蓝色', "颜色4" => '紫色'); echo '<pre class="brush:php;toolbar:false">'; var_dump(array_values($array)); ?>
Output:
array (size=4) 0 => string '红色' (length=6) 1 => string '黄色' (length=6) 2 => string '蓝色' (length=6) 3 => string '紫色' (length=6)
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to change array key name to number in php. For more information, please follow other related articles on the PHP Chinese website!