算法:手写一个删除多维数组中指定value的函数

2020-05-12  本文已影响0人  金星show
function deletedigui($arr,$value){
    if(empty($arr) || !is_array($arr) || empty($value)) return [];

    foreach($arr as $key => &$val){
        if($val == $value){
            unset($arr[$key]);
        }
        if(is_array($val)){
            $val=deletedigui($val,$value);
        }
    }

    return $arr;
}


$arr = ['key1'=>['zjx'],'key2'=>'zjx','key3'=>['key4'=>['key5'=>'zjx','key6'=>'xml']],'key7'=>['zjx','xml'],'key8'=>['key9'=>['key10'=>'zjx','key11'=>['key12'=>'zjx']]]];

var_dump(deletedigui($arr,'zjx'));
上一篇下一篇

猜你喜欢

热点阅读