简体   繁体   English

如何在PHP中取消设置空数组?

[英]how to unset empty arrays in php?

i have an array $mainArray of arrays and i would like to remove / undset the arrays that vave keys with no value. 我有一个数组$mainArray的数组,我想删除/取消设置无键值的数组。

here is my array: 这是我的数组:

Array
(
[0] => Array
    (
        [msg_id] => 203
        [comment] => Array
            (
                [0] => Array
                    (
                        [com_id] => 
                    )
            )
    )
[1] => Array
    (
        [msg_id] => 202
        [comment] => Array
            (
                [0] => Array
                    (
                        [com_id] => 196
                    )
                [1] => Array
                    (
                        [com_id] => 197
                    )
                [2] => Array
                    (
                        [com_id] => 
                    )
            )
    )
[2] => Array
    (
        [msg_id] => 201
        [comment] => Array
            (
                [0] => Array
                    (
                        [com_id] => 198
                    )
                [1] => Array
                    (
                        [com_id] => 
                    )
            )
    )
)

In this case i would like to look inside comment array arrays and see if there are any of them that have empty values. 在这种情况下,我想查看comment数组数组的内部,看看是否有任何具有空值的数组。 The best case scenario would be to remove the comment array entirely if all sub arrays are empty. 最好的情况是,如果所有子数组都为空,则完全删除comment数组。

nut im of with leaving the comment hey there just null 留下comment即时消息嘿嘿只是空

this array should become: 该数组应变为:

Array
(
    [0] => Array
        (
            [msg_id] => 203
        )
    [1] => Array
        (
            [msg_id] => 202
            [comment] => Array
                (
                    [0] => Array
                        (
                            [com_id] => 196
                        )
                    [1] => Array
                        (
                            [com_id] => 197
                        )
                )
        )
    [2] => Array
        (
            [msg_id] => 201
        )
)

any ideas on how to proceed? 关于如何进行的任何想法?

thanks. 谢谢。

Use php's unset() to unset any array key/value. 使用php的unset()取消设置任何数组键/值。

More info on this link http://in3.php.net/unset 此链接的更多信息http://in3.php.net/unset

in your case the code can be, (i have not tested it. but let me know if you have any problem and i can fix it) 在您的情况下,代码可以是(我尚未测试过。但是如果您有任何问题,请告诉我,我可以解决)

function unsetCommentFromArray($mainArray) {
    foreach($mainArray as $key => $value) {
        foreach($value['comment'] as $k => $v) {
            if(empty($v['com_id'])) {
                unset($mainArray[$key]['comment'][$k]);
            }
        }   
    }
    return $mainArray;
}

array_filter() is what you're after. array_filter()是您所追求的。 Particularly a recursive version. 特别是递归版本。 The following was taken from a comment on the PHP Doc :. 以下是对PHP Doc的评论

function array_filter_recursive($array, $callback = null) {
    foreach ($array as $key => & $value) {
        if (is_array($value)) {
            $value = array_filter_recursive($value, $callback);
        }
        else {
            if ( ! is_null($callback)) {
                if ( ! $callback($value)) {
                    unset($array[$key]);
                }
            }
            else {
                if ( ! (bool) $value) {
                    unset($array[$key]);
                }
            }
        }
    }
    unset($value);

    return $array;
}
$array = array_map(function ($i) {
    $i['comment'] = array_filter($i['comment'], function ($c) { return $c['com_id']; });
    return array_filter($i);
}, $array);

Requires PHP 5.3 or higher. 需要PHP 5.3或更高版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM