0
$detalhes->pro_are_id = '["4","1","2","3","6"]'; // Dados foram gravados no banco atraés do json_encode()
$valor->are_id = 2;
if(!empty($detalhes) and isset($detalhes->pro_are_id)){
$are_id = json_decode($detalhes->pro_are_id);
if(array_search($valor->are_id, $are_id)){
$existe = 1;
}
}
The problem is that within the loop with all the selected objects or 1 or 2, it always displays one missing. Example, if I search by id 2,3,6 it only comes to 3 and 4. 2 no longer displays. is always the first id that does not compare.
How to adjust this?
On the whole is this:
<p class="mb20">Selecione uma ou mais áreas</p>
<select id="select-multi" name="pro_are_id[]" data-placeholder="Escolha 1 ou +" multiple="" class="width300 select2-offscreen" tabindex="-1">
<option>Selecione</option>
<?php
$existe = 0;
foreach($listagem_area as $valor){
if(!empty($detalhes) and isset($detalhes->pro_are_id)){
$are_id = json_decode($detalhes->pro_are_id);
if(array_search($valor->are_id, $are_id)){
$existe = 1;
}
}
?>
<option value="<?php echo $valor->are_id; ?>" <?php if($existe==1) echo "selected"; ?> ><?php echo $valor->are_titulo; ?></option>
<?php } ?>
</select>
What is
$listagem_area
?– Woss
The
array_search
returns the found array key, if it is0
theif
will understand howfalse
(due to poor PHP typing). So you should usearray_search($valor->are_id, $are_id) !== false
, This is one of the mistakes. Actually, I could usein_array()
or could use thearray_intersect()
to get the common values in both arrays.– Inkeliz