dongpa3109 2014-06-26 19:27
浏览 37
已采纳

递归删除元素值

So I have the following array:

$array = array(array('fruit1' => 'apple'), 
               array('fruit2' => 'orange'), 
               array('veg1' => 'tomato'), 
               array('veg2' => 'carrot'));

and I want to run a function like this:

array_remove_recursive($array, 'tomato');

so the output is this:

$array = array(array('fruit1' => 'apple'), 
               array('fruit2' => 'orange'), 
               array('veg2' => 'carrot')); // note: the entire array containing tomato has been removed!

Is this solvable?

  • 写回答

3条回答 默认 最新

  • droos02800 2014-06-26 19:49
    关注
    function array_remove_recursive($getArray,$getAssocValue)
    {
        $returnArray    = array();
        if(is_array($getArray))
        {
            foreach($getArray as $indAssocValue)
            {
                if(is_array($indAssocValue))
                {
                    foreach($indAssocValue as $innerKey=>$innerVal)
                    {
                        if($innerVal!=$getAssocValue and $innerKey!="")
                        {
                            $returnArray[]  = array("$innerKey"=>$innerVal);
                        }
                    }
                }
            }
        }
        return $returnArray;
    }
    
    $array = array(array('fruit1' => 'apple'), 
                   array('fruit2' => 'orange'), 
                   array('veg1' => 'tomato'), 
                   array('veg2' => 'carrot'));
                   print_r($array);
                   echo "<br />";
    $array  = array_remove_recursive($array, 'tomato');
    print_r($array);
    

    hope the above code would be helpfull.

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?