Home  >  Article  >  Daily Programming  >  How to remove null values ​​from array in PHP? (Pictures + Videos)

How to remove null values ​​from array in PHP? (Pictures + Videos)

藏色散人
藏色散人Original
2018-10-09 14:06:2710881browse

This article mainly introduces two ways to remove null values ​​from PHP arrays.

Deleting empty arrays in PHP can be said to be a relatively common question among PHP interview questions. So what are the methods for PHP to remove null values ​​from arrays?

Below we will introduce two methods of emptying PHP arrays through specific code examples.

First there is an array data with space values:

<?php
$arr = array(&#39; &#39;, 1, 2, 3, &#39; &#39;, 5);
echo "<pre class="brush:php;toolbar:false">";
print_r($arr);

Access through the browser, print the array as shown below:

How to remove null values ​​from array in PHP? (Pictures + Videos)

Such as There are six elements in the $arr array, and two of the values ​​​​are space elements.

Method 1: Use the array function array_filter to call back the function that filters the empty elements of the array

<?php
$arr = array(&#39; &#39;, 1, 2, 3, &#39; &#39;, 5);
echo "<pre class="brush:php;toolbar:false">";
print_r($arr);
print_r(array_filter($arr, "del"));
function del($var)
{
 return (trim($var));}

In the above code, we define a del method, and then filter the values ​​in the array through the array_filter() function callback function.

The array_filter function passes each key value in the input array to the callback function. If the callback function returns true, the current key value in the input array is returned to the result array. Array key names remain unchanged. The trim() function means removing blank characters or other predefined characters on both sides of the string.

The result is as shown below:

How to remove null values ​​from array in PHP? (Pictures + Videos)

As shown in the picture, we have successfully removed the empty elements with subscripts 0 and 4 in the array through method 1.

Method 2: Use the preg_grep function to detect elements in the array that meet the requirements and return

<?php
$arr = array(&#39; &#39;, 1, 2, 3, &#39; &#39;, 5);
echo "<pre class="brush:php;toolbar:false">";
print_r($arr);
$ptn = "/\S+/i";
print_r(preg_grep($ptn, $arr));

Here we mainly use regular expressions to write matching elements Specify the requirements and then use the preg_grep() function to return array entries that match the pattern. Through this method, the result of emptying the PHP array is finally achieved, as follows:

How to remove null values ​​from array in PHP? (Pictures + Videos)

Some friends may not have clear knowledge about regular expressions. You can refer to the PHP Chinese website Regular Expression Manual Come and learn!

This article is a detailed explanation of the two methods of removing null values ​​​​in arrays in PHP.

To learn more about PHP, you can follow the PHP Chinese website PHP video tutorial!

The above is the detailed content of How to remove null values ​​from array in PHP? (Pictures + Videos). 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