Yii2.0 ArrayHelper::map() 使用方法
来源:互联网 发布:积分管理数据库设计 编辑:程序博客网 时间:2024/06/03 03:22
从多维数组或数组中构建一个映射(键-值 的形式)
通过“$from”和“$to”参数指定对应的键值或属性名称来设置的映射关系。
当然也可以根据分组字段“$group”来进一步分组的映射。
举个例子:
$array = [
['id' => '123', 'name' => 'aaa', 'class' => 'x'],
['id' => '124', 'name' => 'bbb', 'class' => 'x'],
['id' => '345', 'name' => 'ccc', 'class' => 'y'],
];
上面的数组执行以下方法
1
$result
= ArrayHelper::map(
$array
,
'id'
,
'name'
);
得到的结果是
[
'123' => 'aaa',
'124' => 'bbb',
'345' => 'ccc',
]
还可以添加第四个参数
1
$result
= ArrayHelper::map(
$array
,
'id'
,
'name'
,
'class'
);
得到的结果是
[
'x' => [
'123' => 'aaa',
'124' => 'bbb',
],
'y' => [
'345' => 'ccc',
],
]
下面是map方法的详细代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**
* @paramarray $array
* @param string|Closure $from
* @param string|Closure $to
* @param string|Closure $group
* @return array
*/
public
static
function
map(
$array
,
$from
,
$to
,
$group
= null)
{
$result
= [];
foreach
(
$array
as
$element
) {
$key
=
static
:: getValue(
$element
,
$from
);
$value
=
static
:: getValue(
$element
,
$to
);
if
(
$group
!== null) {
$result
[
static
:: getValue(
$element
,
$group
)][
$key
] =
$value
;
}
else
{
$result
[
$key
] =
$value
;
}
}
return
$result
;
}
0 0
- Yii2.0 ArrayHelper::map() 使用方法
- php Yii2.0 ArrayHelper::map() 用法剖析
- yii2.0 ArrayHelper
- Yii2:推荐一个方法arrayHelper::map()
- Yii2 ArrayHelper
- arrayHelper的map()方法
- Yii2.0 Cookie使用方法
- Yii2.0 SESSION使用方法
- YII2.0 Activeform表单组件的使用方法
- YII2.0 Activeform表单组件的使用方法
- yii2.0 Activeform表单部分组件使用方法
- yii2.0 Activeform表单部分组件使用方法
- yii2.0 Activeform表单部分组件使用方法
- yii2.0 Activeform表单部分组件使用方法
- YII2.0 表单组件的使用方法
- YII2.0 Activeform表单组件的使用方法
- YII2.0 Activeform表单组件的使用方法
- YII2.0 表单组件的使用方法
- 解决libc.so.6: version `GLIBC_2.14′ not found问题
- PKU 2255 Tree Recovery(二叉树)
- 天梯赛练习——帅到没朋友
- Rxjava 从入门到开发
- ORA-12560: TNS: 协议适配器错误
- Yii2.0 ArrayHelper::map() 使用方法
- FFmpeg发送流媒体的命令(UDP,RTP,RTMP)
- 剑指offer:树的子结构
- ajax中error函数参数详解
- pthread_create()创建线程最大个数
- ArrayList循环遍历并删除元素的常见陷阱
- 开通博客学图像处理
- errno.h头文件介绍
- python的中文文本挖掘库snownlp进行购物评论文本情感分析实例