duanqiang6501 2011-03-27 15:17
浏览 41
已采纳

PHP从关联数组中删除元素

I have an PHP array that looks something like this:

Index              Key     Value
[0]                1       Awaiting for Confirmation
[1]                2       Assigned
[2]                3       In Progress
[3]                4       Completed
[4]                5       Mark As Spam

When I var_dump the array values i get this:

array(5) { [0]=> array(2) { ["key"]=> string(1) "1" ["value"]=> string(25) "Awaiting for Confirmation" } [1]=> array(2) { ["key"]=> string(1) "2" ["value"]=> string(9) "Assigned" } [2]=> array(2) { ["key"]=> string(1) "3" ["value"]=> string(11) "In Progress" } [3]=> array(2) { ["key"]=> string(1) "4" ["value"]=> string(9) "Completed" } [4]=> array(2) { ["key"]=> string(1) "5" ["value"]=> string(12) "Mark As Spam" } }

I wanted to remove Completed and Mark As Spam. I know I can unset[$array[3],$array[4]), but the problem is that sometimes the index number can be different.

Is there a way to remove them by matching the value name instead of the key value?

  • 写回答

9条回答 默认 最新

  • dtdr84101 2011-03-27 15:19
    关注

    Your array is quite strange : why not just use the key as index, and the value as... the value ?

    Wouldn't it be a lot easier if your array was declared like this :

    $array = array(
        1 => 'Awaiting for Confirmation', 
        2 => 'Asssigned', 
        3 => 'In Progress', 
        4 => 'Completed', 
        5 => 'Mark As Spam', 
    );
    

    That would allow you to use your values of key as indexes to access the array...

    And you'd be able to use functions to search on the values, such as array_search() :

    $indexCompleted = array_search('Completed', $array);
    unset($array[$indexCompleted]);
    
    $indexSpam = array_search('Mark As Spam', $array);
    unset($array[$indexSpam]);
    
    var_dump($array);
    

    Easier than with your array, no ?



    Instead, with your array that looks like this :

    $array = array(
        array('key' => 1, 'value' => 'Awaiting for Confirmation'), 
        array('key' => 2, 'value' => 'Asssigned'), 
        array('key' => 3, 'value' => 'In Progress'), 
        array('key' => 4, 'value' => 'Completed'), 
        array('key' => 5, 'value' => 'Mark As Spam'), 
    );
    

    You'll have to loop over all items, to analyse the value, and unset the right items :

    foreach ($array as $index => $data) {
        if ($data['value'] == 'Completed' || $data['value'] == 'Mark As Spam') {
            unset($array[$index]);
        }
    }
    var_dump($array);
    

    Even if do-able, it's not that simple... and I insist : can you not change the format of your array, to work with a simpler key/value system ?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(8条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?