2017-07-23 120 views
0

我想測試一個數組是否包含一個值。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語句我可以指定返回總是假的。 爲什麼?

+0

因爲'in_array'不知道內部結構'composants' –

+0

@u_mulder,我做了什麼? –

+0

做別的事。 –

回答

0

您必須將您的$composants值平整爲nom值。

如果$composants不是從Collection對象,嘗試創建一個和採摘

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

否則,你可以簡單地這樣做:

$composants = $composants->pluck('nom'); 
相關問題