Php laravel刀片:测试数组是否包含值

Php laravel刀片:测试数组是否包含值,php,laravel,blade,Php,Laravel,Blade,我想测试数组是否包含值 我有两个数组: var_dump($ingredients) array (size=12) 0 => object(stdClass)[303] public 'id' => int 21 public 'nom' => string 'Tomate' (length=6) public 'created_at' => null public 'updated_at' => nu

我想测试数组是否包含值

我有两个数组:

var_dump($ingredients)
array (size=12)
  0 => 
    object(stdClass)[303]
      public 'id' => int 21
      public 'nom' => string 'Tomate' (length=6)
      public 'created_at' => null
      public 'updated_at' => null
  1 => 
    object(stdClass)[111]
      public 'id' => int 22
      public 'nom' => string 'Carotte' (length=7)
      public 'created_at' => null
      public 'updated_at' => null
  2 => 
    object(stdClass)[302]
      public 'id' => int 23
      public 'nom' => string 'Pommes' (length=6)
      public 'created_at' => null
      public 'updated_at' => null
  3 => 
    object(stdClass)[309]
      public 'id' => int 24
      public 'nom' => string 'Noix' (length=4)
      public 'created_at' => null
      public 'updated_at' => null
  4 => 
    object(stdClass)[310]
      public 'id' => int 25
      public 'nom' => string 'Poivrons' (length=8)
      public 'created_at' => null
      public 'updated_at' => null
  5 => 
    object(stdClass)[311]
      public 'id' => int 26
      public 'nom' => string 'Oignons' (length=7)
      public 'created_at' => null
      public 'updated_at' => null
  6 => 
    object(stdClass)[312]
      public 'id' => int 27
      public 'nom' => string 'Anchoix' (length=7)
      public 'created_at' => null
      public 'updated_at' => null
  7 => 
    object(stdClass)[313]
      public 'id' => int 28
      public 'nom' => string 'Roquette' (length=8)
      public 'created_at' => null
      public 'updated_at' => null
  8 => 
    object(stdClass)[314]
      public 'id' => int 29
      public 'nom' => string 'Thon' (length=4)
      public 'created_at' => null
      public 'updated_at' => null
  9 => 
    object(stdClass)[315]
      public 'id' => int 30
      public 'nom' => string 'Parmesan' (length=8)
      public 'created_at' => null
      public 'updated_at' => null
  10 => 
    object(stdClass)[316]
      public 'id' => int 31
      public 'nom' => string 'Piments fort' (length=12)
      public 'created_at' => string '2017-07-23 07:03:55' (length=19)
      public 'updated_at' => string '2017-07-23 07:03:55' (length=19)
  11 => 
    object(stdClass)[317]
      public 'id' => int 32
      public 'nom' => string 'cacahuétes' (length=11)
      public 'created_at' => string '2017-07-23 07:06:31' (length=19)
      public 'updated_at' => string '2017-07-23 07:06:31' (length=19)

var_dump($composant)    
array (size=3)

0 =>     object(stdClass)[321]
      public 'nom' => string 'Anchoix' (length=7)

1 => 
    object(stdClass)[322]
      public 'nom' => string 'Roquette' (length=8)

2 => 
    object(stdClass)[323]
      public 'nom' => string 'Thon' (length=4)
我想检查$composant中包含哪些成分,以便我可以指定是否必须选中该复选框

@foreach($ingredients as $item)
                    @if(in_array($item->nom,$composants,true))
                        {!! Form::checkbox($item->nom,$item->nom, true) !!} {!! $item->nom !!}
                    @else
                        {!! Form::checkbox((string)$item->nom,$item->nom, false) !!} {!! $item->nom !!}
                    @endif
        @endforeach
但是我的if语句返回的总是错误的。
为什么

如果您的
$composants
不是来自集合对象,请尝试创建一个集合对象,然后将其删除

// will only select the 'nom' values
$composants = collect($composants)->pluck('nom');
否则,您可以简单地执行以下操作:

$composants = $composants->pluck('nom');

因为
in_array
不知道
组件的内部结构以及我该做什么??@u_mulder你帮了我很多…当然,现在你知道
in_array
对你没有帮助了)组件的值是多少?