Home >Backend Development >PHP Problem >How to remove 0 elements from an array in php

How to remove 0 elements from an array in php

青灯夜游
青灯夜游Original
2021-10-25 18:55:412915browse

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])".

How to remove 0 elements from an array in php

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:

How to remove 0 elements from an array in php

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!

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