假设我有一个多维数字零索引数组,如下所示:
$oldArray = (
0 => array("importantKey" => "1", "otherKey" => "someValue"),
1 => array("importantKey" => "4", "otherKey" => "someValue"),
);
如果我可以确定“importantKey”的唯一性,那么将其映射到以下内容的最简洁的方法是什么
$newArray = (
1 => array("otherKey" => "someValue"),
4 => array("otherKey" => "someValue"),
);
在对“importantKey”执行 GROUP BY 子句后从数据库中检索多行时,这很有用
最佳答案
试试这个
$newArray = array_reduce($oldArray, function($res, $val) {
$res[$val['importantKey']]['otherKey'] = $val['otherKey'];
return $res;
}, array());
这样够优雅吗? :)
关于php - 从包含元素的键的值枚举多维 PHP 数组上的键的最优雅的方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16276645/