In PHP, you can use the array_values() function to convert an associative array into a normal array (index array), and the syntax is "array_values($array)". The array_values() function returns an array containing all the values in the given array, but does not retain the key names; the returned array will be in the form of an index array, and the index of the array starts from 0 and increases by 1.
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In the PHP array, no matter what type of key Each name will have a value corresponding to it, that is, a key/value pair. According to the different data types of the array key names, PHP arrays can be divided into two types:
Using numbers as keys The name is called an Indexed Array - an ordinary array
An array with a string or a mixture of strings and numbers as keys is called an Associative Array.
php method to convert an associative array into a normal array (index array)
In PHP, you can use the array_values() function to convert an associative array into an ordinary array (index array) The array is converted into a normal array (indexed 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 $array
, it can return a value containing all 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.
array_values($array)
array_values() function is particularly suitable for arrays with confusing element subscripts, or for converting associative arrays into indexed arrays.
Code sample:
<?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); ?>
Note: The array_values() function only applies to one-dimensional arrays, for multi-dimensional arrays other than the first dimension Dimensions have no effect.
But you can get the value by accessing the array in the multidimensional array:
<?php header("Content-type:text/html;charset=utf-8"); $arr = array(1,2,3,"aa"=>array(4,5,6),7,8,array(9,10)); echo "原关联数组:"; var_dump($arr); $res=array_values($arr["aa"]); echo "转换后的数组:"; var_dump($res); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to convert associative array to ordinary array in php. For more information, please follow other related articles on the PHP Chinese website!