PHP 中带键的数组映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12050892/
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 02:42:45  来源:igfitidea点击:

Array mapping in PHP with keys

phparraysmap

提问by lorenzo-s

Just for curiosity (I know it can be a single line foreachstatement), is there some PHP array function (or a combination of many) that given an array like:

只是出于好奇(我知道它可以是单行foreach语句),是否有一些 PHP 数组函数(或多个函数的组合)给出了一个数组,例如:

Array (
    [0] => stdClass Object (
        [id] => 12
        [name] => Lorem
        [email] => [email protected]
    )
    [1] => stdClass Object (
        [id] => 34
        [name] => Ipsum
        [email] => [email protected]
    )
)

And, given 'id'and 'name', produces something like:

并且,给定'id'and 'name',产生类似的东西:

Array (
    [12] => Lorem
    [34] => Ipsum
)

I use this pattern a lot, and I noticed that array_mapis quite useless in this scenario cause you can't specify keys for returned array.

我经常使用这种模式,我注意到array_map在这种情况下这非常无用,因为您无法为返回的数组指定键。

回答by moonwave99

Just use array_reduce:

只需使用array_reduce

$obj1 = new stdClass;
$obj1 -> id = 12;
$obj1 -> name = 'Lorem';
$obj1 -> email = '[email protected]';

$obj2 = new stdClass;
$obj2 -> id = 34;
$obj2 -> name = 'Ipsum';
$obj2 -> email = '[email protected]';

$reduced = array_reduce(
    // input array
    array($obj1, $obj2),
    // fold function
    function(&$result, $item){ 
        // at each step, push name into $item->id position
        $result[$item->id] = $item->name;
        return $result;
    },
    // initial fold container [optional]
    array()
);

It's a one-liner out of comments ^^

这是一个单行评论^^

回答by lorenzo-s

I found I can do:

我发现我可以做到:

array_combine(array_map(function($o) { return $o->id; }, $objs), array_map(function($o) { return $o->name; }, $objs));

But it's ugly and requires two whole cycles on the same array.

但它很丑陋,需要在同一个阵列上进行两个完整的循环。

回答by Josua Marcel Chrisano

Because your array is array of object then you can call (its like a variable of class) try to call with this:

因为你的数组是对象数组,所以你可以调用(它就像一个类变量)尝试用这个调用:

 foreach ($arrays as $object) {
    Echo $object->id;
    Echo "<br>";
    Echo $object->name;
    Echo "<br>";
    Echo $object->email;
    Echo "<br>";
 } 

Then you can do

然后你可以做

 // your array of object example $arrays;
 $result = array();
 foreach ($arrays as $array) {
       $result[$array->id] = $array->name;
 }

   echo "<pre>";
   print_r($result);
   echo "</pre>";

Sorry I'm answering on handphone. Can't edit the code

对不起,我是用手机接听的。无法编辑代码

回答by Athari

The easiest way is to use a LINQ port like YaLinqolibrary*. It allows performing SQL-like queries on arrays and objects. Its toDictionaryfunction accepts two callbacks: one returning key of the result array, and one returning value. For example:

最简单的方法是使用 LINQ 端口,如YaLinqo库*。它允许对数组和对象执行类似 SQL 的查询。它的toDictionary函数接受两个回调:一个返回结果数组的键,一个返回值。例如:

$userNamesByIds = from($users)->toDictionary(
    function ($u) { return $u->id; },
    function ($u) { return $u->name; }
);

Or you can use a shorter syntax using strings, which is equivalent to the above version:

或者您可以使用字符串使用更短的语法,这相当于上面的版本:

$userNamesByIds = from($users)->toDictionary('$v->id', '$v->name');

If the second argument is omitted, objects themselves will be used as values in the result array.

如果省略第二个参数,则对象本身将用作结果数组中的值。

* developed by me

* 由我开发

回答by Sachin Sarola

The easiest way is to use an array_column()

最简单的方法是使用 array_column()

$result_arr = array_column($arr, 'name', 'id');
print_r($result_arr );