gpt4 book ai didi

php - Laravel 集合包含

转载 作者:行者123 更新时间:2023-12-02 23:10:00 25 4
gpt4 key购买 nike

我正在集合上使用 Laravel contains 方法 https://laravel.com/docs/5.3/collections#method-contains 。但这对我不起作用。

foreach ($this->options as $option) {
if($options->contains($option->id)) {
dd('test');
}
}

dd($options); 看起来像这样:

Collection {#390
#items: array:1 [
0 => array:3 [
0 => array:7 [
"id" => 10
"slug" => "test"
"name" => "test"
"poll_id" => 4
"created_at" => "2016-11-12 20:42:42"
"updated_at" => "2016-11-12 20:42:42"
"votes" => []
]
1 => array:7 [
"id" => 11
"slug" => "test-1"
"name" => "test"
"poll_id" => 4
"created_at" => "2016-11-12 20:42:42"
"updated_at" => "2016-11-12 20:42:42"
"votes" => []
]
2 => array:7 [
"id" => 12
"slug" => "test-2"
"name" => "test"
"poll_id" => 4
"created_at" => "2016-11-12 20:42:42"
"updated_at" => "2016-11-12 20:42:42"
"votes" => []
]
]
]
}

dd($option->id);的结果是10

可能出了什么问题?或者有更好的办法吗?

最佳答案

您应该将键/值对传递给 contains 方法,这将确定给定的对是否存在于集合中。使用contains()方法如下:

foreach ($this->options as $option) {
// Pass key inside contains method
if($option->contains('id', $option->id)) {
dd('test');
}
}

关于php - Laravel 集合包含,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40566972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com