用键在PHP中映射数组

时间:2021-12-15 10:45:28

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

仅为了好奇(我知道它可以是每个语句的一行),是否有某个PHP数组函数(或多个数组的组合)给一个数组,比如:

Array (
    [0] => stdClass Object (
        [id] => 12
        [name] => Lorem
        [email] => lorem@example.org
    )
    [1] => stdClass Object (
        [id] => 34
        [name] => Ipsum
        [email] => ipsum@example.org
    )
)

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

如果给定'id'和'name',则产生如下内容:

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

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

我经常使用这种模式,我注意到array_map在这个场景中非常没用,因为您不能为返回的数组指定键。

4 个解决方案

#1


20  

Just use array_reduce:

只是使用的形式:

$obj1 = new stdClass;
$obj1 -> id = 12;
$obj1 -> name = 'Lorem';
$obj1 -> email = 'lorem@example.org';

$obj2 = new stdClass;
$obj2 -> id = 34;
$obj2 -> name = 'Ipsum';
$obj2 -> email = 'ipsum@example.org';

$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 ^^

这是一个一行程序的注释^ ^

#2


2  

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.

但是它很丑,需要在同一个数组上有两个完整的循环。

#3


0  

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

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

#4


0  

The easiest way is to use a LINQ port like YaLinqo library*. It allows performing SQL-like queries on arrays and objects. Its toDictionary function 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

*由我开发的

#1


20  

Just use array_reduce:

只是使用的形式:

$obj1 = new stdClass;
$obj1 -> id = 12;
$obj1 -> name = 'Lorem';
$obj1 -> email = 'lorem@example.org';

$obj2 = new stdClass;
$obj2 -> id = 34;
$obj2 -> name = 'Ipsum';
$obj2 -> email = 'ipsum@example.org';

$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 ^^

这是一个一行程序的注释^ ^

#2


2  

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.

但是它很丑,需要在同一个数组上有两个完整的循环。

#3


0  

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

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

#4


0  

The easiest way is to use a LINQ port like YaLinqo library*. It allows performing SQL-like queries on arrays and objects. Its toDictionary function 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

*由我开发的