For example:
$searchParameters = array(
'parameter1' => 'value1',
'parameter2' => 'value2',
'parameter3' => 'value3',
);
$allowedParamters = array('parameter1','parameter2');
What's the best way to get $searchParameters
containing only the allowed pairs?
foreach($searchParameters as $key => $value) {
if(!array_key_exists($key, $allowedParamters)) {
unset($searchParameters[$key];
}
}
This will delete the not-allowed keys and their value.
Use array_intersect_key()
:
$searchParameters = array(
'parameter1' => 'value1',
'parameter2' => 'value2',
'parameter3' => 'value3',
);
$allowedParamters = array('parameter1','parameter2');
$result = array_intersect_key($searchParameters, array_flip($allowedParamters));
foreach($searchParameters as $key=>$value){
if(in_array($key,$allowedParamters)){
$trueResult[$key] = $value;
}
}
print_r($trueResult);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.