laravel blade:测试数组是否包含值

时间:2017-07-23 16:28:21

标签: php laravel blade

我想测试数组是否包含值。

我有2个阵列:

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语句总是返回false。  为什么????

1 个答案:

答案 0 :(得分:0)

您必须将$composants值展平为nom个值。

如果您的$composants不是来自Collection对象,请尝试创建一个并选择

// will only select the 'nom' values
$composants = collect($composants)->pluck('nom');

否则,你可以这样做:

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