#PHP How to remove key values from an array?
In PHP, you can use the "array_values()" function to remove key values from the array. This function will return all the values in the array. Its syntax is "array_values(array)", and its parameter array represents The array to be operated on, the return value is an indexed array containing all values.
Sample code
<?php $array = array("size" => "XL", "color" => "gold"); print_r(array_values($array)); ?>
The above routine will output:
Array ( [0] => XL [1] => gold )
<?php /** * Get all values from specific key in a multidimensional array * * @param $key string * @param $arr array * @return null|string|array */ function array_value_recursive($key, array $arr){ $val = array(); array_walk_recursive($arr, function($v, $k) use($key, &$val){ if($k == $key) array_push($val, $v); }); return count($val) > 1 ? $val : array_pop($val); } $arr = array( 'foo' => 'foo', 'bar' => array( 'baz' => 'baz', 'candy' => 'candy', 'vegetable' => array( 'carrot' => 'carrot', ) ), 'vegetable' => array( 'carrot' => 'carrot2', ), 'fruits' => 'fruits', ); var_dump(array_value_recursive('carrot', $arr)); // array(2) { [0]=> string(6) "carrot" [1]=> string(7) "carrot2" } var_dump(array_value_recursive('apple', $arr)); // null var_dump(array_value_recursive('baz', $arr)); // string(3) "baz" var_dump(array_value_recursive('candy', $arr)); // string(5) "candy" var_dump(array_value_recursive('pear', $arr)); // null ?>
Recommended tutorial: "PHP》
The above is the detailed content of How to remove key values from an array in PHP?. For more information, please follow other related articles on the PHP Chinese website!