[英]Push values on to a key in an associative array
Given: 鉴于:
$groups = EveMarketGroupsEntity::distinct()->select('name')->get();
$groupInformation = [];
if (!is_null($groups)) {
foreach ($groups as $group) {
}
}
How do I do the following properly 如何正确执行以下操作
if (!is_null($groups)) {
foreach ($groups as $group) {
$groupInformation[$group->name][]= EveMarketGroupsEntity::where('name', $group->name)->first()->item();
}
}
Right now I get: 现在我得到:
array:818 [▼
"Weaponry" => array:1 [▶]
...
]
When Weaponry
should have more then 1 element. 当
Weaponry
需要多于1个元素时。 In fact: 事实上:
array:818 [▼
"Weaponry" => array:1 [▶]
"Special Edition Industrial Ships" => array:1 [▶]
"Nanite Injectors" => array:1 [▶]
"Vehicles and Gear" => array:1 [▶]
"Rapid Light Missile Launchers" => array:1 [▶]
"Standard Light Missiles" => array:1 [▶]
"Jump Bridge" => array:1 [▶]
"Missile Launcher Rigs" => array:1 [▶]
"Strip Miners" => array:1 [▶]
"Freighters" => array:1 [▶]
...
]
They should all have more then 1 element. 它们都应该包含1个以上的元素。 What am I missing?
我想念什么?
Question: How do I push the same element on to the same key thus creating an array: array('key' => [elm, elm, elm ...])
问题:如何将相同的元素推到相同的键上,从而创建一个数组:
array('key' => [elm, elm, elm ...])
So, (Feedback is welcome, especially if you down vote this) 因此,(欢迎反馈,尤其是如果您对此投反对票)
foreach ($groups as $group) {
$groupInformation[$group->name] = EveMarketGroupsEntity::where('name', $group->name)->first()->item()->get()->all();
}
This worked for me ... 这对我有用...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.