Yii2:推荐一个方法arrayHelper::map()

arrayHelper::map()这个方法可以将一个数组拆成一个键-值对映射的多维数组或对象数组。

例子:

 1 $array = [
 2     ['id' => '123', 'name' => 'aaa', 'class' => 'x'],
 3     ['id' => '124', 'name' => 'bbb', 'class' => 'x'],
 4     ['id' => '345', 'name' => 'ccc', 'class' => 'y'],
 5 );
 6 
 7 $result = ArrayHelper::map($array, 'id', 'name');
 8 // the result is:
 9 // [
10 //     '123' => 'aaa',
11 //     '124' => 'bbb',
12 //     '345' => 'ccc',
13 // ]
14 
15 $result = ArrayHelper::map($array, 'id', 'name', 'class');
16 // the result is:
17 // [
18 //     'x' => [
19 //         '123' => 'aaa',
20 //         '124' => 'bbb',
21 //     ],
22 //     'y' => [
23 //         '345' => 'ccc',
24 //     ],
25 // ]
原文地址:https://www.cnblogs.com/qmsu/p/4581358.html