Array mapping in PHP with keys

后端 未结 5 1445
日久生厌
日久生厌 2020-12-30 04:59

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:

5条回答
  •  不思量自难忘°
    2020-12-30 05:28

    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.

提交回复
热议问题