Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fredmoras's avatar

test if an array contain a value.

hello,

I would like to test if an array contain a value.

I have 2 array : ''' 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)

''' I would like to check which ingredients is in $composant so i can specify if the checkbox must been checked or unchecked

@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

but my if statements return always false. Why????

0 likes
0 replies

Please or to participate in this conversation.