Comparison of PHP Array()

Asked

Viewed 73 times

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 arrTag equals the id inside arrBanco and put echo "checked";

It summarizes in a list of the database, where I have a list of items (pacotes_items) and will be marked the items that have already been purchased (pacotes_itens_purchased).

HTML TO BE INSERTED CHECKED. Within the 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>
  • where are these variables? arrtag, arrbanco...

  • They are in the above question... I just don’t know how to check inside the arrays, if the id of one is equal to the id of the other

  • within the larger code, where both variables are?

  • 2

    What do you want with this array representing within a string? You’re thinking of using the eval?

  • Put the code that has the arrays in the question. With those two strings you can’t know what you want. You need to put the real arrays, not the visual representation of them..

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 difficulty understanding the relationship between "$dados->detalhes_pacote" and "$arrTag $arrBanco"...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.