1
我的下一个集合数组:收集到关联特定键[15.2]
Collection {#356 ▼
#items: array:31 [▼
0 => {#359 ▼
+"id": 17
+"zone_id": 2
+"name_de": "Österreich"
+"name_en": "Austria"
+"name_iso": "AUSTRIA"
+"tld": "at"
+"iso3166": "AT"
+"phone": 43
+"vat_regex": "/^U[0-9]{8}$/"
+"shop_id": 17
+"country_id": 165
}
1 => {#360 ▼
+"id": 2
+"zone_id": 2
+"name_de": "Belgien"
+"name_en": "Belgium"
+"name_iso": "BELGIUM"
+"tld": "be"
+"iso3166": "BE"
+"phone": 32
+"vat_regex": "/^[01][0-9]{9}$/"
+"shop_id": 17
+"country_id": 25
}]
}
我想接下来的结果作为关联数组:
[
"AT" => "Austria",
"BE" => "Belgium"
]
我尝试使用做到这一点:
$keyed = $countries->map(function ($item) {
return [$item->iso3166 => $item->name_en];
});
但我发现了:
Collection {#357 ▼
#items: array:31 [▼
0 => array:1 [▼
"AT" => "Austria"
]
1 => array:1 [▼
"BE" => "Belgium"
]
]
}
我在做什么错了,或者我该如何实现关联数组?
注意:我正在使用Laravel 5.2所以mapWithKeys()收集方法未实现。
因为这样,我不知道拔毛方法。谢谢。 – Troyer
@Troyer是的,收集非常整洁;还有一本关于收藏的免费书籍https://leanpub.com/laravelcollectionsunraveled玩得开心! :) – Kyslik