Foreach inside foreach (Php - Laravel) inside if give double outputs -


i want disable input checkboxes.

$value1->name has values(product1, product2, product3, product4, product5) $value2->name has values(product3, product4) @foreach ($products_table1 $value1)     @foreach($products_table2 $value2)         @if($value1->name != $value2->name)             disabled         @endif     @endforeach @endforeach 

i want output result this:

<input type="checkbox" disabled> product 1 <input type="checkbox" disabled> product 2 <input type="checkbox" > product 3 <input type="checkbox" > product 4 <input type="checkbox" disabled> product 5 

but instead of get:

<input type="checkbox" disabled disabled > product 1 <input type="checkbox" disabled disabled > product 2 <input type="checkbox" disabled > product 3 <input type="checkbox" disabled > product 4 <input type="checkbox" disabled disabled > product 5 

what can prevent double output? there alternatives options? trying create product filter checkboxes.

you create array-diff first; creating list of products in value1 not in value2 , looping new array only:

http://php.net/manual/en/function.array-diff.php

the other option avoids explicitly allocating difference array use boolean operator 'in-array':

http://php.net/manual/en/function.in-array.php

either of these should more efficient original, since you're noticing, you're comparing each set of 2 items twice in nested foreach


Comments