Home >Backend Development >PHP Problem >How to remove 0 elements from an array in php
In php, you can use the array_diff() function to remove 0 elements in the array. This function is used to compare the values of two arrays and return the difference set. You only need to use an array containing "0" and Just compare the original array; the syntax format is "array_diff($arr, [0])".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php removes the array Elements that are 0
In PHP, you can use the array_diff() function to remove 0 elements from the array. Syntax: array_diff($arr, [0])
Example:
<?php // 清除数组中指定元素 $arr = [1,2,3,0,1]; $arr = array_diff($arr, [0]);//输出[1,2,3,1] var_dump($arr); ?>
Output result:
Description:
array_diff() function is used to compare the values of two (or more) arrays and return the difference set.
This function compares the values of two (or more) arrays (key=>value in value), and returns a difference array, which includes all the arrays being compared (array1 ), but not in any other parameter array (array2 or array3, etc.).
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove 0 elements from an array in php. For more information, please follow other related articles on the PHP Chinese website!