Array $ arr [1 & amp; gt; & #039; aaa & #039;, 2 & amp; gt; & #039; bbb & #039 ;, 3 & amp; gt; & #039; ccc & #039;, 4 & amp; gt; & #039; ddd & #039;]; $ keys [1, 3]; ask if there is any ready-made function to get the entry of $ arr whose key is 1 and 3, and form a new array to return. do You Have To foreach? Requirement... Array
$arr=[ 1=>'aaa', 2=>'bbb', 3=>'ccc', 4=>'ddd'];$keys=[1,3];
Ask if there is any ready-made function to get the entry of $ arr whose key is 1 and 3, and form a new array to return.
Do You Have To foreach?
====================================
Demand changes
To obtain $ keys = [1, 3, 3];
That is to say, there will be duplicate keys. Please ~
3q
Reply content:
Array
$arr=[ 1=>'aaa', 2=>'bbb', 3=>'ccc', 4=>'ddd'];$keys=[1,3];
Ask if there is any ready-made function to get the entry of $ arr whose key is 1 and 3, and form a new array to return.
Do You Have To foreach?
====================================
Demand changes
To obtain $ keys = [1, 3, 3];
That is to say, there will be duplicate keys. Please ~
3q
$arr=[ 1=>'aaa', 2=>'bbb', 3=>'bbb', 4=>'bbb'];$keys=[1,3];$result = array_intersect_key($arr, array_fill_keys($keys, 0));print_r($result);
1.
$arr_tmp = array($arr['1'],$arr['3']);
2.
array_filter($arr,funtion($v,$k) use ($keys){ return isset($keys[$k])?true:false;});
Code not tested