php 如何将 array_map 与键和值一起使用,但返回具有相同索引(不是 int)的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25513361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:51:57  来源:igfitidea点击:

How can I use array_map with keys and values, but return an array with the same indexes (not int)?

phparraysassociative-arrayarray-map

提问by user2816456

I have an array such as ['id' => 1, 'name' => 'Fred'].

我有一个数组,例如['id' => 1, 'name' => 'Fred'].

I want to call array_mapon this array and also use the key inside the function. However, when I make a return, my keys will become int.

我想调用array_map这个数组并使用函数内的键。但是,当我返回时,我的密钥将变为 int。

Simple example :

简单的例子:

$arr = array('id' => 1, 'name' => 'Fred');
$result = array_map(
    function ($value, $key) {
        return $value;
     },
     $arr,
     array_keys($arr)
);
var_dump($result);

Basically, I want $resultto be identical to $arrin this case, but it turns my string keys into ints.

基本上,我想在这种情况下$result$arr此相同,但它将我的字符串键转换为整数。

回答by Jannie Theunissen

For your requirement of "$result to be identical to $arr", try:

对于“$result 与 $arr 相同”的要求,请尝试:

$result = array_combine(
     array_keys($arr), 
     array_map(function($v){ return $v; }, $arr)
);

Gives:

给出:

   [
     "id" => 1
     "name" => "Fred"
   ]

回答by Marcos

The closest you will get using array_map()is this:

您将使用的最接近的array_map()是:

<?php
$arr = array('id'=>1,'name'=>'Jon');

$callback = function ($key, $value) {
    return array($key => $value);
  };

$arr = array_map( $callback, array_keys($arr), $arr);
var_dump($arr);
?>

Gives:

给出:

   [
     [
       "id" => 1
     ],
     [
       "name" => "Jon"
     ]
   ]

You will be better creating your own function with a foreachinside.

您将更好地使用foreach内部创建自己的功能。

回答by user3550312

Based on @Jannie Theunissen answer the correct way of getting an array_map working with key for comparing and assigning values based on second array for example is:

基于@Jannie Theunissen 的回答,让 array_map 与键一起工作以根据第二个数组进行比较和分配值的正确方法是:

$result = array_combine(
 array_keys($arr), 
 array_map(function($v, $key){ return $v; }, $arr, array_keys($arr))
);

Or for optimized alternative:

或者对于优化的替代方案:

$keys = array_keys($arr);
$result = array_combine(
 $keys, 
 array_map(function($v, $key){ return $v; }, $arr, $keys)
);

And with a comparison array value:

并使用比较数组值:

$compareArray = [/*same structure as $arr but with specific values*/];
$keys = array_keys($arr);
$result = array_combine(
 $keys, 
 array_map(function($v, $key) use ($compareArray) { 
              // recursive can be achieved here passing $v and $compareArray[$key]
              return $compareArray[$key]; 
           }, $arr, $keys)
);

回答by Radon8472

What you need is array_walk. Try this code:

你需要的是array_walk。试试这个代码:

$arr = array('id' => 1, 'name' => 'Fred');
array_walk(
    $arr,
    function (&$value, $key) {
        // do stuff
     }
);
print_r($arr);

Unfortunally it works not when you try to change the keys. But you can change the values when you pass them by refference.

不幸的是,当您尝试更改密钥时它不起作用。但是当您通过引用传递它们时,您可以更改这些值。

If you musst change the keys too check Question to array_walk-change-keysand the first answer:

如果您也必须更改键,请检查问题到array_walk-change-keys和第一个答案: