laravel 在集合上使用 in_array
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38105073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using in_array on collections
提问by CFree
I need to check for a name in an array of names but I having trouble passing an array instead of a collection to the in_array() method.
我需要检查名称数组中的名称,但我无法将数组而不是集合传递给 in_array() 方法。
My blade code looks something like this
我的刀片代码看起来像这样
@foreach($ecn->areas as $area)
{{ $area->area }}:<br>
<ul>
@foreach($area->people as $person)
@if(in_array($person, $ecn->signatures->name ))
<li><del>{{ $person }}</del></li>
@else
<li>{{ $person }}</li>
@endif
@endforeach
</ul>
@endforeach
I know my problem is in the way im trying to access the list of signatures.
我知道我的问题在于我试图访问签名列表的方式。
@if(in_array($person, $ecn->signatures->name ))
I can access them in another part of the page doing this
我可以在页面的另一部分访问它们
@foreach($ecn->signatures as $signature)
{{ $signature->name }}<br>
@endforeach
and everything is fine.
一切都很好。
How can I access and pass the list of signatures as an array in this scenario?
在这种情况下,如何以数组的形式访问和传递签名列表?
回答by Hassan Azimi
If you want to use in_array()
version for Laravel Collections then you can use:
如果你想使用in_array()
Laravel Collections 的版本,那么你可以使用:
$collection->contains($needle)
It woks just like in_array
.
它就像in_array
.
If $needle
is an integer, an id
for example, don't forget to pluck first, like so:
如果$needle
是整数,id
例如,不要忘记先拔,像这样:
$collection->pluck('id')->contains($needle)
回答by patricus
You can use the lists
or pluck
method on the signatures collection to get a new collection of all the names. From there, you can either use the contains()
method on the collection, or you can use the all()
method to turn the collection into an array and use in_array
directly.
您可以在签名集合上使用lists
orpluck
方法来获取所有名称的新集合。从那里,您可以contains()
在集合上使用该方法,也可以使用该all()
方法将集合转换为数组并in_array
直接使用。
I would suggest doing this outside of the foreach loop, so you're not generating this new collection and array every iteration.
我建议在 foreach 循环之外执行此操作,因此您不会在每次迭代时生成这个新集合和数组。
Array:
大批:
// controller
$names = $ecn->signatures->lists('name')->all();
// blade
@if(in_array($person, $names))
Collection:
收藏:
// controller
$names = $ecn->signatures->lists('name');
// blade
@if($names->contains($person))
You can also use contains()
with a closure, which would let you get away with not creating an intermediate collection, but it's a little harder to read:
您还可以使用contains()
闭包,这样您就可以避免创建中间集合,但阅读起来有点困难:
Contains with closure:
包含关闭:
// blade
@if($ecn->signatures->contains(function ($key, $value) use ($person) {
return $value->name == $person;
}))
回答by Akshay Naik
As per Laravel Docs
根据 Laravel 文档
The
get
method returns an array of results where each result is an instance of the PHPStdClass
object.
该
get
方法返回一个结果数组,其中每个结果都是 PHPStdClass
对象的一个实例。
So you must convert your stdClass object to a array in your controller itself.
因此,您必须将 stdClass 对象转换为控制器本身中的数组。
foreach ($ecn->signatures as $signature)
$nameArray[] = $signature->name;
and then you can use in_array in the blade template
然后你可以在刀片模板中使用 in_array
@if(in_array($person, $nameArray))