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

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大