Good day. Tell me, I need to output one array in the form of a checkbox, marking the checkboxes whose values ​​are in the second array.

There are arrays

$prop = Array ( [values] => Array ( [0] => Пластиковые окна [1] => Натяжные потолки [2] => Установка дверей [3] => Укладка ламината [4] => Оклейка обоев ) ) $option_key = Array ( [0] => Натяжные потолки [1] => Оклейка обоев [2] => Пластиковые окна ); 

It is necessary to display $ prop array in the form of flags, but at the same time, those values ​​that are in the $ option_key array should be noted.

I tried this way, he notes everything to me:

 foreach ($prop as $k => $v) { foreach ($v as $values) { print "<label><input"; if (isset($prop[$k])) { print " checked "; } print "type='checkbox' name='$k'> $values</label>"; } } 

    1 answer 1

    something like this?

     foreach($prop as $k => $p){ $checked = in_array($p, $option_key) ? "checked" : ""; echo "<label><input type='checkbox' name='$k' $checked>$p</label>"; } 
    • one
      Stop. And why in 1 function argument in_array - do you pass an array in which you need to do a search? It is incorrectly written - it is necessary so: in_array($p, $option_key) - that is, to swap places. - And
    • @And because I don't remember the order of the arguments of all the functions by heart (: I wanted to check, but I was distracted. I corrected. - teran
    • I only get one checkbox with the Array value - Batyabest
    • @teran maybe I am doing something wrong, foreach one turns out needed? - Batyabest
    • @Batyabest means you $prop has a different look than you wrote, but something like $prop = [ 0 => [ 'Пластиковые окна', '...', ... ]] - teran