使用特定列中的值替换第一级数组键

发布于 2024-12-04 20:35:57 字数 1431 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深者入戏 2024-12-11 20:35:57

现代答案(需要 PHP 5.5)

新函数 array_column< /a> 非常通用,它可以做的事情之一就是这种类型的重新索引:

// second parameter is null means we 're just going to reindex the input
$usersById = array_column($users, null, 'Id');

原始答案(对于早期的 PHP 版本)

您需要使用 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);

查看它行动

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:

// second parameter is null means we 're just going to reindex the input
$usersById = array_column($users, null, 'Id');

Original answer (for earlier PHP versions)

You need to fetch the ids from the sub-arrays with array_map, then create a new array with array_combine:

$ids = array_map(function($user) { return $user['Id']; }, $users);
$users = array_combine($ids, $users);

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:

$ids = array_map(create_function('$user', 'return $user["Id"];'), $users);
$users = array_combine($ids, $users);

See it in action.

∞觅青森が 2024-12-11 20:35:57

您可以使用 array_reduce() 函数,例如:

$usersById = array_reduce($users, function ($reduced, $current) {
    $reduced[$current['Id']] = $current;
    return $reduced;
});

但是,它并不比一个foreach

You could use the array_reduce() function, like:

$usersById = array_reduce($users, function ($reduced, $current) {
    $reduced[$current['Id']] = $current;
    return $reduced;
});

However, it's no more elegant than a foreach.

柒夜笙歌凉 2024-12-11 20:35:57

我认为使用 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:

$keyed = array();
foreach($users as $w) $keyed[$w['Id']] = $w;

In case you want to replace the existing array, foreach is not that flexible indeed. But maybe the following is some sort of alternative:

$users = function($key) use ($users)
{
    foreach($users as $v) $keys[] = $v[$key];
    return array_combine($keys, $users);
};
$users = $users('Id');

It allows the callback to accept parameters, e.g. the name of the key which should be used to create the new keys from.

故人如初 2024-12-11 20:35:57

还有一种使用 array_walk 的变体:

$usersById = array();
array_walk($users, function($val) use (&$usersById) {
    $usersById[$val['Id']] = $val;
});

And one more variant using array_walk:

$usersById = array();
array_walk($users, function($val) use (&$usersById) {
    $usersById[$val['Id']] = $val;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文