Removal steps: 1. Use the foreach statement to traverse the array, the syntax is "foreach ($array as $k => $v){//Loop body statement block;}"; 2. In the loop body, Use is_numeric() to determine whether the element is a number. If so, use the unset() function to delete it. The syntax is "if (is_numeric($v)) {unset($arr[$k]);}".
The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer
In PHP, you can use the foreach statement and is_numeric () and unset() functions to remove numeric elements from an array.
Implementation steps:
Step 1. Use the foreach statement to traverse the array
foreach ($array as $k => $v){ //循环体语句块; }
Traverse to A certain $arr array, in each loop, the value of the current array will be assigned to $v, and the key name will be assigned to $k.
Step 2: In the loop body, use is_numeric() to determine whether the element is a number. If so, use the unset() function to delete it
is_numeric() function is used to detect whether a variable is a number or a numeric string. If the specified variable is a number or numeric string, it returns TRUE, otherwise it returns FALSE. Note that floating point type returns 1, which is TRUE.
The unset() function is used to destroy the given variable.
if (is_numeric($v)) { unset($arr[$k]); }
Implementation example code:
<?php header('content-type:text/html;charset=utf-8'); function f($arr) { foreach ($arr as $k => $v) { if (is_numeric($v)) { unset($arr[$k]); } } echo "去除数字元素后"; var_dump($arr); } $arr = array(1,2,"3","hello",null,'',"b"); var_dump($arr); f($arr); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove numeric elements from php array. For more information, please follow other related articles on the PHP Chinese website!