PHP - 从数组中删除空值[重复]

时间:2021-09-24 13:03:18

This question already has an answer here:

这个问题在这里已有答案:

Current array:

Array (
    [name] => Array (
        [name1] => Array (
            [0] => some value1
        )
        [name2] => Array (
            [0] => some value2
        )
        [name3] => Array (
            [0] =>
        )
) 

Wanted array:

Array (
    [name] => Array (
        [name1] => Array (
            [0] => some value1
        )
        [name2] => Array (
            [0] => some value2
        )
) 

Since name3[0] doesn't contain any value, it needs to be removed. From what I read, I should use array_filter for this, but I can't get it to work.

由于name3 [0]不包含任何值,因此需要将其删除。从我读到的,我应该使用array_filter,但我不能让它工作。

3 个解决方案

#1


4  

Recursive function, it will remove all empty values and empty arrays from input array:

递归函数,它将从输入数组中删除所有空值和空数组:

//clean all empty values from array
function cleanArray($array)
{
    if (is_array($array))
    {
        foreach ($array as $key => $sub_array)
        {
            $result = cleanArray($sub_array);
            if ($result === false)
            {
                unset($array[$key]);
            }
            else
            {
                $array[$key] = $result;
            }
        }
    }

    if (empty($array))
    {
        return false;
    }

    return $array;
}

I have tested it on this example, it works no matter how deep is array:

我在这个例子中测试了它,无论数组有多深,它都可以工作:

$array = array(
    'name' => array(
        'name1' => array(0 => 1),
        'name2' => array(0 => 3, 1 => array(5 => 0, 1 => 5)),
        'name3' => array(0 => '')
    )
);

Hope this helps :)

希望这可以帮助 :)

#2


5  

You need to feed array_filter a predicate (function) that determines if the [0] sub-element of each array element is empty or not. So:

您需要为array_filter提供一个谓词(函数),该谓词确定每个数组元素的[0]子元素是否为空。所以:

$array = array_filter($array, function($item) { return !empty($item[0]); });

Be aware that empty is not very picky: it will result in removing any item whose [0] sub-element is the empty string, false, null, 0 or "0" -- it will also remove items that do not have a [0] sub-element at all. If you need something more surgically targeted the test needs to be fine-tuned.

请注意,empty不是很挑剔:它会导致删除[0]子元素为空字符串,false,null,0或“0”的任何项目 - 它也会删除没有[ 0]子元素。如果您需要更符合手术目标的测试,则需要对测试进行微调。

#3


2  

Could be accomplished using a recursive function:

可以使用递归函数完成:

$arr = array('test', array('',0,'test'), array('',''));

print_r(clean_array($arr));

function clean_array($array, $isRepeat = false) {
    foreach ($array as $key => $value) {
        if ($value === null || $value === '') { unset($array[$key]); }
        else if (is_array($value)) {
            if (empty($value)) { unset($array[$key]); }
            else $array[$key] = clean_array($value);
        }
    }
    if (!$isRepeat) {
        $array = clean_array($array,true);
    }
    return $array;
}

#1


4  

Recursive function, it will remove all empty values and empty arrays from input array:

递归函数,它将从输入数组中删除所有空值和空数组:

//clean all empty values from array
function cleanArray($array)
{
    if (is_array($array))
    {
        foreach ($array as $key => $sub_array)
        {
            $result = cleanArray($sub_array);
            if ($result === false)
            {
                unset($array[$key]);
            }
            else
            {
                $array[$key] = $result;
            }
        }
    }

    if (empty($array))
    {
        return false;
    }

    return $array;
}

I have tested it on this example, it works no matter how deep is array:

我在这个例子中测试了它,无论数组有多深,它都可以工作:

$array = array(
    'name' => array(
        'name1' => array(0 => 1),
        'name2' => array(0 => 3, 1 => array(5 => 0, 1 => 5)),
        'name3' => array(0 => '')
    )
);

Hope this helps :)

希望这可以帮助 :)

#2


5  

You need to feed array_filter a predicate (function) that determines if the [0] sub-element of each array element is empty or not. So:

您需要为array_filter提供一个谓词(函数),该谓词确定每个数组元素的[0]子元素是否为空。所以:

$array = array_filter($array, function($item) { return !empty($item[0]); });

Be aware that empty is not very picky: it will result in removing any item whose [0] sub-element is the empty string, false, null, 0 or "0" -- it will also remove items that do not have a [0] sub-element at all. If you need something more surgically targeted the test needs to be fine-tuned.

请注意,empty不是很挑剔:它会导致删除[0]子元素为空字符串,false,null,0或“0”的任何项目 - 它也会删除没有[ 0]子元素。如果您需要更符合手术目标的测试,则需要对测试进行微调。

#3


2  

Could be accomplished using a recursive function:

可以使用递归函数完成:

$arr = array('test', array('',0,'test'), array('',''));

print_r(clean_array($arr));

function clean_array($array, $isRepeat = false) {
    foreach ($array as $key => $value) {
        if ($value === null || $value === '') { unset($array[$key]); }
        else if (is_array($value)) {
            if (empty($value)) { unset($array[$key]); }
            else $array[$key] = clean_array($value);
        }
    }
    if (!$isRepeat) {
        $array = clean_array($array,true);
    }
    return $array;
}