How to remove numeric elements from php array

青灯夜游
Release: 2023-03-16 20:14:02
Original
1906 people have browsed it

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){
    //循环体语句块;
}
Copy after login
  • 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]);
}
Copy after login

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);
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!