Array_map multiple array to map, lost key?

Source: Internet
Author: User
Hi, all

$myarr=["22"=>1,"33"=>2]

When I use

$return=array_map(function($v,$k){    //TODO},$myarr,array_keys($myarr));

How do $return keep the original key?

I found that for

array_map(function($v){    //TODO},$myarr);

It is possible to keep the key.

Reply content:

Hi, all

$myarr=["22"=>1,"33"=>2]

When I use

$return=array_map(function($v,$k){    //TODO},$myarr,array_keys($myarr));

How do $return keep the original key?

I found that for

array_map(function($v){    //TODO},$myarr);

It is possible to keep the key.

Array_map () This function when there is only one parameter in the callback function (that is, when there is only one array), the callback function operates independently of the value of the array, or it can return a key, but when Array_map () operates two arrays, you also return the key? It is impossible to think about it, the function is the key to return the first array? Or the key of the second array? So multiple arrays will not give you the key, just give you return value ...
1 want to ask is, what kind of function do you want to realize?
2 If you want to save the key value of the first array, you can do this:

$arr1 = array('b' => 'banana', 'g' => 'grage');$arr2 = array('a' => 'apple', 'p' => 'pear');$return = array_map(function($a, $b){    // to do something    return 'I love ' . $a . ' and ' . $b;}, $arr1, $arr2);$return = array_combine(array_keys($arr1), $return);
  • Related Article

    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.