Is there any neat way to make an array that its keys comes from the first array and its values come from the second, and discarding any item in the second that does not have any corresponding key in the first? for example if:
$keys = array('key1'=>1,'key2'=>2,'key3'=>3);
$vals = array('key1'=>'val1', 'key4'=>'val4');
the result would be:
$result=array('key1'=>'val1' , 'key2'=>2 , 'key3'=>3);
Currently, I am using these two lines:
$result = array_intersect_key($vals, $keys);
$result = array_merge($vals, $result);
Is there any cleaner way to do that?
You're doing it correctly as is. To reduce a line you could do away with reassigning the $result
variable like
$result = array_merge($vals, array_intersect_key($vals, $keys));
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.