doxn43207 2016-11-03 12:13
浏览 65
已采纳

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

I have a multidimensional which is a dynamic array like :

Array
(
    [0] => Array
        (
            [key] => delete
            [label] => hi Delete
        )

    [1] => Array
        (
            [key] => edit
            [label] => hi Edit
        )

    [2] => Array
        (
            [key] => update
            [label] => hi update
        )

)

now i want to delete an array below from above multidimensional array:

Array
    (
        [key] => delete
        [label] => hi Delete
    )

finally i want an output like:

Array (

    [0] => Array
        (
            [key] => edit
            [label] => hi Edit
        )

    [1] => Array
        (
            [key] => update
            [label] => hi update
        )

)

For this i have tried, below is my code:

<?php
  $arr1 = array(array("key" => "delete", "label" => "hi Delete"),array("key" => "edit", "label" => "hi Edit"), array("key" => "update", "label" => "hi update"));
   $diff = array_diff_assoc($arr1, array("key" => "delete", "label" => "hi Delete"));
   print_r($diff);
?>

But i get full $arr1 in the output:

Array
(
    [0] => Array
        (
            [key] => delete
            [label] => hi Delete
        )

    [1] => Array
        (
            [key] => edit
            [label] => hi Edit
        )

    [2] => Array
        (
            [key] => update
            [label] => hi update
        )

)

how can i do this please help me

  • 写回答

2条回答 默认 最新

  • dsla94915 2016-11-03 12:29
    关注

    Use array_filter with callback as

    $arr1 = array_filter($arr1, function ($var) {
        return $var['key'] != 'delete';
    });
    print_r($arr1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信
  • ¥30 Python调用dll文件输出Nan重置dll状态