PHP obtains the specified part of the array.

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.