$v){//Loop body statement block;}"; 2. In the loop body, use is_numeric() to determine whether the element is It is a number. If it is, use the unset() function to delete it. The syntax is "if (is_numeric($v)) {unset($arr[$k]);}"."/> $v){//Loop body statement block;}"; 2. In the loop body, use is_numeric() to determine whether the element is It is a number. If it is, use the unset() function to delete it. The syntax is "if (is_numeric($v)) {unset($arr[$k]);}".">

Home  >  Article  >  Backend Development  >  How to remove numeric elements from php array

How to remove numeric elements from php array

青灯夜游
青灯夜游Original
2022-09-19 19:17:081758browse

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]);}".

How to remove numeric elements from php array

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(&#39;content-type:text/html;charset=utf-8&#39;);
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,&#39;&#39;,"b");
var_dump($arr);
f($arr);
?>

How to remove numeric elements from php array

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn