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

青灯夜游
Release: 2023-03-13 13:34:01
Original
2911 people have browsed it

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

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template