Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
新函数 array_column< /a> 非常通用,它可以做的事情之一就是这种类型的重新索引:
array_column
// second parameter is null means we 're just going to reindex the input $usersById = array_column($users, null, 'Id');
您需要使用 array_map,然后使用 array_combine:
array_map
array_combine
$ids = array_map(function($user) { return $user['Id']; }, $users); $users = array_combine($ids, $users);
上面的代码需要 PHP > ;= 5.3 对于匿名函数语法,但是您也可以使用 < 执行相同操作(尽管看起来有点难看) code>create_function 仅需要 PHP >= 4.0.1:
$ids = array_map(create_function('$user', 'return $user["Id"];'), $users); $users = array_combine($ids, $users);
查看它行动。
The new function array_column is very versatile and one of the things it can do is exactly this type of reindexing:
You need to fetch the ids from the sub-arrays with array_map, then create a new array with array_combine:
The code above requires PHP >= 5.3 for the anonymous function syntax, but you can also do the same (albeit it will look a bit uglier) with create_function which only requires PHP >= 4.0.1:
create_function
See it in action.
您可以使用 array_reduce() 函数,例如:
array_reduce()
$usersById = array_reduce($users, function ($reduced, $current) { $reduced[$current['Id']] = $current; return $reduced; });
但是,它并不比一个foreach。
foreach
You could use the array_reduce() function, like:
However, it's no more elegant than a foreach.
我认为使用 foreach 更加优雅。也许你只是想以不同的方式编写它:
$keyed = array(); foreach($users as $w) $keyed[$w['Id']] = $w;
如果你想替换现有的数组,foreach确实不是那么灵活。但也许以下是某种替代方案:
$users = function($key) use ($users) { foreach($users as $v) $keys[] = $v[$key]; return array_combine($keys, $users); }; $users = $users('Id');
它允许回调接受参数,例如用于创建新密钥的密钥名称。
I think using foreach is much more elegant. Maybe you just only want to write it differently:
In case you want to replace the existing array, foreach is not that flexible indeed. But maybe the following is some sort of alternative:
It allows the callback to accept parameters, e.g. the name of the key which should be used to create the new keys from.
还有一种使用 array_walk 的变体:
$usersById = array(); array_walk($users, function($val) use (&$usersById) { $usersById[$val['Id']] = $val; });
And one more variant using array_walk:
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
暂无简介
文章 0 评论 0
接受
发布评论
评论(4)
现代答案(需要 PHP 5.5)
新函数
array_column
< /a> 非常通用,它可以做的事情之一就是这种类型的重新索引:原始答案(对于早期的 PHP 版本)
您需要使用
array_map
,然后使用array_combine
:上面的代码需要 PHP > ;= 5.3 对于匿名函数语法,但是您也可以使用 < 执行相同操作(尽管看起来有点难看) code>create_function 仅需要 PHP >= 4.0.1:
查看它行动。
Modern answer (requires PHP 5.5)
The new function
array_column
is very versatile and one of the things it can do is exactly this type of reindexing:Original answer (for earlier PHP versions)
You need to fetch the ids from the sub-arrays with
array_map
, then create a new array witharray_combine
:The code above requires PHP >= 5.3 for the anonymous function syntax, but you can also do the same (albeit it will look a bit uglier) with
create_function
which only requires PHP >= 4.0.1:See it in action.
您可以使用
array_reduce()
函数,例如:但是,它并不比一个
foreach
。You could use the
array_reduce()
function, like:However, it's no more elegant than a
foreach
.我认为使用
foreach
更加优雅。也许你只是想以不同的方式编写它:如果你想替换现有的数组,
foreach
确实不是那么灵活。但也许以下是某种替代方案:它允许回调接受参数,例如用于创建新密钥的密钥名称。
I think using
foreach
is much more elegant. Maybe you just only want to write it differently:In case you want to replace the existing array,
foreach
is not that flexible indeed. But maybe the following is some sort of alternative:It allows the callback to accept parameters, e.g. the name of the key which should be used to create the new keys from.
还有一种使用 array_walk 的变体:
And one more variant using array_walk: