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

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

Oct 09, 2018 pm 02:06 PM

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

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

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

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)