$v){if (!$v){unset($arr[$k]);}}". 2. Use the array_filter() function to filter the associative array and delete null elements (all elements in the array whose value is equal to FALSE). The syntax is "array_filter($arr)"."/> $v){if (!$v){unset($arr[$k]);}}". 2. Use the array_filter() function to filter the associative array and delete null elements (all elements in the array whose value is equal to FALSE). The syntax is "array_filter($arr)".">

Home >Backend Development >PHP Problem >How to remove null values ​​from php associative array

How to remove null values ​​from php associative array

青灯夜游
青灯夜游Original
2022-06-29 19:37:121934browse

Two removal methods: 1. Use the foreach statement to loop through the associative array, and use the unset() function in the loop body to delete the null value elements according to the key name. The syntax "foreach($arr as $k= >$v){if(!$v){unset($arr[$k]);}}". 2. Use the array_filter() function to filter the associative array and delete null elements (all elements in the array whose value is equal to FALSE). The syntax is "array_filter($arr)".

How to remove null values ​​from php associative array

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer

php in associative array Two methods to remove null values

Method 1: foreach statement unset() function

1. Use the foreach statement to loop through Associative array

foreach ($array as $key => $value){
    循环语句块;
}

Traverses the given $array array, and in each loop, the value of the current array is assigned to $value, and the key name is assigned to $key.

2. In the loop body, determine whether the array elements are null values ​​one by one, and use the unset() function to delete the null values ​​based on the key names

if( !$value ){
unset( $arr[$key] );  
}

Full code:

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(1=>"1","a"=>"",2=>"2","b"=>0,"c"=>"blue");
var_dump($arr);
foreach ($arr as $k=>$v){
    if( !$v ){
		unset($arr[$k]);  
	}
}
var_dump($arr);
?>

How to remove null values ​​from php associative array

Method 2: Use the array_filter() function to filter the associative array and delete null value elements

array_filter() function, also Called a callback function, it is used to filter the elements of an array using a user-defined function. It iterates over each value in the array, passing them to a user-defined function or callback function.

When the array_filter() function is used to declare a callback function, it will delete false values ​​(null values), but if the callback function is not specified, all elements in the array with a value equal to FALSE, such as null, will be deleted String or NULL value.

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(11,&#39;&#39;,null,12,false,0); 
var_dump($arr);
$newarr = array_filter($arr); 
echo "过滤后的数组:";
var_dump($newarr);
?>

How to remove null values ​​from php associative array

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to remove null values ​​from php associative 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