Comparison of Array () PHP

0

I have the following arrays:

$arrTag = "Array ( [0] => stdClass Object ( [id] => 1 [id_pacote] => 7 [id_fornecedor] => [nome] => Salgados - 100 Un [valor_compra] => 100 [valor_venda] => 150 [descricao] => Pacote de Salgados com 100 unidades [created_at] => 2016-11-03 18:00:55 [updated_at] => ) [1] => stdClass Object ( [id] => 2 [id_pacote] => 7 [id_fornecedor] => [nome] => Doces - 100 Un [valor_compra] => 50 [valor_venda] => 114 [descricao] => Pacote com 100 unidades de doces [created_at] => 2016-11-03 18:01:31 [updated_at] => ) [2] => stdClass Object ( [id] => 3 [id_pacote] => 7 [id_fornecedor] => [nome] => Refrigerante - 10 un [valor_compra] => 50 [valor_venda] => 85 [descricao] => Pacote com 10 unidades de refrigerantes [created_at] => 2016-11-03 18:02:04 [updated_at] => ) )";
$arrBanco = "Array ( [0] => stdClass Object ( [valor_venda] => 150 [id] => 1 ) [1] => stdClass Object ( [valor_venda] => 85 [id] => 3 ) )";

I need to compare if the id inside the arrTag, is equal to the id inside the Array and put echo "checked";

It comes down to a list of the database, where I have a list of items (package_items) and will be marked the items that have already been bought (packages_items_comprados).

HTML THAT WILL BE INSERTED OR CHECKED. Within input type="checkbox"

<table class="table table-bordered">
    <tr>
        <th width="50" class="text-center">#</th>
        <th>Item do Pacote</th>
        <th>Valor</th>
        <th width="50" class="text-center"></th>
    </tr>
    <?php 
    if(count($dados->detalhes_pacote)>0){
        foreach($dados->detalhes_pacote as $valor_pacote){                                      
    ?>
        <tr>
            <td width="50" class="text-center"><?php echo $valor_pacote->id; ?></td>
            <td><?php echo $valor_pacote->nome; ?></td>
            <td>R$ <?php echo number_format($valor_pacote->valor_venda, "2", ",", "."); ?></td>
            <td width="50" class="text-center"><input type="checkbox" name="itens[]" id="itens[]" class="checados" data-id="<?php echo $valor_pacote->id; ?>" value="<?php echo number_format($valor_pacote->valor_venda, "2", ",", "."); ?>|<?php echo $valor_pacote->id; ?>"></td>
        </tr>
    <?php 
        }
    } else {
    ?>
        <tr>
            <td colspan="4" class="text-center">Nenhum item foi adicionado a este pacote.</td>
        </tr>
    <?php } ?>
</table>
    
asked by anonymous 03.11.2016 / 18:58

1 answer

2
foreach ($arrBanco as $objBanco) {
  foreach ($arrTag as &$objtag) {

    if($objTag->id == $objBanco->id){
      // encontrei o $objTag, pode altera-lo diretamente aqui...
      $objTag->jaComprado = true;
    }

  }
}

I had trouble understanding the relationship between "$ data-> package_parts" and "$ arrTag $ arrBank" ...

    
03.11.2016 / 20:04