search
HomeBackend DevelopmentPHP ProblemHow to remove spaces from php array

How to remove spaces from php array

Aug 15, 2020 pm 02:54 PM
phpspace

How to remove spaces from PHP arrays: Use the function [array_filter()] to remove spaces from a one-dimensional array. The code is [function filter_array($arr, $values ​​= ['', null, false, 0, '0',[]])].

How to remove spaces from php array

How to remove spaces from an array in php:

To remove empty values ​​from an array in php, you can use array_filter()This function can only work on one-dimensional arrays. Once you need to remove empties from a multi-dimensional array, it will not work, and the removed empties also include (int)0, (string)0. Use it There is still something wrong!

The custom function to remove a value in the array defaults to a null value. It can be used for both one-dimensional arrays and multi-dimensional arrays!

/**
 * 去除多维数组中的空值
 * @author 
 * @return mixed
 * @param $arr 目标数组
 * @param array $values 去除的值  默认 去除  '',null,false,0,'0',[]
 */
function filter_array($arr, $values = ['', null, false, 0, '0',[]]) {
    foreach ($arr as $k => $v) {
        if (is_array($v) && count($v)>0) {
            $arr[$k] = filter_array($v, $values);
        }
        foreach ($values as $value) {
            if ($v === $value) {
                unset($arr[$k]);
                break;
            }
        }
    }
    return $arr;
}

Related learning recommendations: php programming (video)

The above is the detailed content of How to remove spaces from php array. 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

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

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.