duanji9378 2018-01-31 06:37
浏览 102
已采纳

如何从多维数组中删除空数组?

I have an array like :

Array
(
    [0] => Array
        (
            [id] => 1367
            [category_value] => 15
            [isepisode] => 0
            [title] => hello world
        )

    [1] => Array
        (
            [id] => 9892
            [category_value] => 
            [isepisode] => 0
            [title] => 
        )
    [2] => Array
        (
            [id] => 9895
            [category_value] => 
            [isepisode] => 0
            [title] => Bla Bla
        )
)

I want to remove array those title is empty.

Here is my code:

$res = array_map('array_filter', $data);
print_r(array_filter($res)); exit;

Above code only removing those array values are empty. But I want to remove whole array which title is null. My output should be:

Array
(
    [0] => Array
        (
            [id] => 1367
            [category_value] => 15
            [isepisode] => 0
            [title] => hello world
        )
    [2] => Array
        (
            [id] => 9895
            [category_value] => 
            [isepisode] => 0
            [title] => Bla Bla
        )
)

Note: I have thousands of array. If put foreach this will take time to execute. Is there any easiest way to do that?

  • 写回答

4条回答 默认 最新

  • duancheng7743 2018-01-31 06:46
    关注

    The right array_filter() approach:

    $result = array_filter($arr, function($a){ return $a['title']; });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?