Home >Backend Development >PHP Problem >How to remove key (key name) from PHP one-dimensional array
Two removal methods: 1. Use the array_values() function to remove the key (key name), the syntax is "array_values (array)". 2. Define an empty array, use the foreach statement to loop through the original array, and pass the key values of the original array into the empty array in the loop body. The syntax is "foreach(original array as $v){$empty array name[]= $v;}".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
php one-dimensional array Two methods to remove key (key name)
Method 1: Use array_values() function to remove
array_values() function in PHP You can get the values of all elements in the array. The syntax of this function is as follows:
array_values($array)
The parameter $array is the array being operated on.
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, you can return an array containing all the values in the given array. Array, but does not retain 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() function is particularly suitable for arrays with confusing element subscripts, or for converting associative arrays into indexed arrays.
Example: array_values() function removes keys from the array
<?php header('content-type:text/html;charset=utf-8'); $arr=array("Peter"=>65,"Harry"=>80,"John"=>78,"Clark"=>90); var_dump($arr); var_dump(array_values($arr)); ?>
Output result:
##Method 2: Use foreach Loop and remove an empty array
<?php $arr1=array("aaa"=>11,"bbb"=>22,"ccc"=>33); var_dump($arr1); $arr2=array(); foreach($arr1 as $v){ $arr2[]=$v; } var_dump($arr2); ?>Recommended learning: "
PHP Video Tutorial"
The above is the detailed content of How to remove key (key name) from PHP one-dimensional array. For more information, please follow other related articles on the PHP Chinese website!