从多维数组或数组中构建一个映射(键-值 的形式)
通过“$from”和“$to”参数指定对应的键值或属性名称来设置的映射关系。
当然也可以根据分组字段“$group”来进一步分组的映射。
举个例子:
$array = [ ['id' => '123', 'name' => 'aaa', 'class' => 'x'], ['id' => '124', 'name' => 'bbb', 'class' => 'x'], ['id' => '345', 'name' => 'ccc', 'class' => 'y'], ];
上面的数组执行以下方法
还可以添加第四个参数 $result = ArrayHelper::map($array, 'id', 'name', 'class');
得到的结果是
[ 'x' => [ '123' => 'aaa', '124' => 'bbb', ], 'y' => [ '345' => 'ccc', ], ]
下面是map方法的详细代码
欢迎使用“起航点卡销售系统”销售程序:一、系统优势 1、售卡系统采取了会员与非会员相结合的销售方法,客户无需注册即可购卡,亦可注册会员购卡。 2、购卡速度快,整个购卡或过程只需二步即可取卡,让客户感受超快的取卡方式! 3、批量加卡功能。 4、取卡方式:网上支付,即时取卡 ,30秒可完成交易。 5、加密方式:MD5 32位不可倒推加密 6、防止跨站
0
/**
* @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;
}以上就是Yii2.0 ArrayHelper::map() 使用方法的内容,更多相关内容请关注PHP中文网(www.php.cn)!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号