replace array keys with given respective keys

前端 未结 10 1372
轻奢々
轻奢々 2020-12-29 09:16

I have an array like below

$old = array(
       \'a\' => \'blah\',
       \'b\' => \'key\',
       \'c\' => \'amazing\',
       \'d\' => array(
          


        
10条回答
  •  再見小時候
    2020-12-29 09:56

    Adapting @shawn-k solution, here is more cleaner code using array_walk, it will only replace desired keys, of course you can modify as per your convenience

    array_walk($old, function($value,$key)use ($keyReplaceInfoz,&$old){
            $newkey = array_key_exists($key,$keyReplaceInfoz)?$keyReplaceInfoz[$key]:false;
            if($newkey!==false){$old[$newkey] = $value;unset($old[$key]);
            }
        });
    
        print_r($old);
    

提交回复
热议问题