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条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号