【发布时间】:2020-05-14 23:45:42
【问题描述】:
我有这段代码,我想将集合重命名为不同的键和值。
但是当我使用map 方法时,值返回到它们现有的key 并且我想删除使用映射method 展平多维集合的键
检索模型:
$user = User::find(123)->orderByDesc('created_at')->get()->pluck('name', 'id');
$data = $user->map(function ($value, $key) {
return [
'id' => $key,
'text' => $value,
];
});
预期结果:
$data = [
[
'id' => 3,
'text' => 'Shinka Nibutani',
], [
'id' => 2,
'text' => 'Kashiwagi Rein',
], [
'id' => 1,
'text' => 'Alice Zuberg',
],
]
实际结果:
$data = [
3 => [
'id' => 3,
'text' => 'Shinka Nibutani',
],
2 => [
'id' => 2,
'text' => 'Kashiwagi Rein',
],
3 => [
'id' => 1,
'text' => 'Alice Zuberg',
],
]
【问题讨论】:
标签: arrays laravel dictionary collections