In PHP, you can use the array_values() function to convert an associative array into an index array. You can use this function to reset the array key name and convert the string or numerically confusing key name to one starting from 0 and Numeric key name incremented by 1; use syntax "array_values($array)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
In PHP, you can use the array_values() function to convert an associative array into an indexed array.
array_values($array)
The function is to return the values of all elements in the array. It is very simple to use. With only one required parameter, you can return a value containing all the elements in the given array. Array of values, but no key names. The returned array will be in the form of an indexed array, with array indices starting at 0 and increasing by 1.
Simply put, you can use this function to reset the array key name and convert the key name with confusing string or numerical value into a numeric key name starting from 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.
Example: Use the array_values() function to convert an associative array into an indexed array
<?php $arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90); var_dump($arr); var_dump(array_values($arr)); ?>
Output result:
If you want to use array_values() to reset the index of a multi-dimensional array, you can refer to the article "PHP array learning how to reset the index of a multi-dimensional array"
Recommended study: "PHP video tutorial》
The above is the detailed content of How to convert associative array to index array in php. For more information, please follow other related articles on the PHP Chinese website!