In PHP, how can I remove items in an array whose keys are found in another array conveniently? -
i have 2 arrays:
$array = new array('id'=>1, 'name'=>'peter', 'sex'=>'male', 'age'=>25); $excludes = new array('sex', 'age');
i want following result:
$array = new array('id'=>1, 'name'=>'peter');
the items keys found in array $excludes removed.
how can achieve conveniently?
simply array_diff_key
, array_flip
functions:
// $arr initial array (besides, don't give `$array` name arrays) $result = array_diff_key($arr, array_flip($excludes)); print_r($result);
the output:
array ( [id] => 1 [name] => peter )
Comments
Post a Comment