例如:
$searchParameters = array(
'parameter1' => 'value1',
'parameter2' => 'value2',
'parameter3' => 'value3',
);
$allowedParamters = array('parameter1','parameter2');
让$searchParameters
仅包含允许的对的最佳方法是什么?
答案 0 :(得分:1)
foreach($searchParameters as $key => $value) {
if(!array_key_exists($key, $allowedParamters)) {
unset($searchParameters[$key];
}
}
这将删除不允许的密钥及其值。
答案 1 :(得分:1)
$searchParameters = array(
'parameter1' => 'value1',
'parameter2' => 'value2',
'parameter3' => 'value3',
);
$allowedParamters = array('parameter1','parameter2');
$result = array_intersect_key($searchParameters, array_flip($allowedParamters));
答案 2 :(得分:0)
foreach($searchParameters as $key=>$value){
if(in_array($key,$allowedParamters)){
$trueResult[$key] = $value;
}
}
print_r($trueResult);