how to echo the array using the foreach

0

I have a variable with $curva that I counted a array , if I am a var_dump the result is this:

 array (size=6)
  0 => 
    array (size=4)
      'id' => string '107' (length=3)
      'nome' => string 'LAREIRA PEQ.' (length=12)
      'qtd' => float 43
      'total' => float 64500
  1 => 
    array (size=4)
      'id' => string '108' (length=3)
      'nome' => string 'CHORAQUERIA PEQ.' (length=16)
      'qtd' => float 60
      'total' => float 48000
  2 => 
    array (size=4)
      'id' => string '109' (length=3)
      'nome' => string 'JOGO DE FACAS' (length=13)
      'qtd' => float 90
      'total' => float 27000
  3 => 
    array (size=4)
      'id' => string '111' (length=3)
      'nome' => string 'PROVALEIRA' (length=10)
      'qtd' => float 100
      'total' => float 6000
  4 => 
    array (size=4)
      'id' => string '110' (length=3)
      'nome' => string 'COOLERS' (length=7)
      'qtd' => float 84
      'total' => float 21000
  5 => 
    array (size=4)
      'id' => string '112' (length=3)
      'nome' => string 'CHAMPANHEIRA' (length=12)
      'qtd' => float 28
      'total' => float 1962.64

I tried to do this:

foreach ($curva as $c) {
        ?>
        <tr>
            <td><?= $c->id ?></td>
            <td><?= $c->nome ?></td>
            <td><?= $c->qtd ?></td>
            <td><?= $c->total ?></td>
        </tr>
        <?php
    }

But it is not working. Does anyone know what it can be?

    
asked by anonymous 07.03.2017 / 18:18

1 answer

4

The -> is used by stdClass and normal class, not by arrays, in array we use array[chave] , it should look like this:

<tr>
    <td><?= $c['id'] ?></td>
    <td><?= $c['nome'] ?></td>
    <td><?= $c['qtd'] ?></td>
    <td><?= $c['total'] ?></td>
</tr>

A hint for when to mix html and php, but it's just my opinion , is to use #

<?php foreach ($curva as $c): ?>
    <tr>
        <td><?= $c['id'] ?></td>
        <td><?= $c['nome'] ?></td>
        <td><?= $c['qtd'] ?></td>
        <td><?= $c['total'] ?></td>
    </tr>
<?php endforeach; ?>

Extra

I noticed that in your other code you did this:

while ($resultado = mysqli_fetch_object($consulta)) {
    $curva[] = array(
        "id" => $resultado->id,
        "nome" => $resultado->nome,
        "qtd" => $qtd,
        "total" => $total
    );
}

If you simplify this way:

while ($resultado = mysqli_fetch_object($consulta)) {
    $curva[] = $resultado;
}

It will be possible to use this:

<?php foreach ($curva as $c): ?>
    <tr>
        <td><?= $c->id ?></td>
        <td><?= $c->nome ?></td>
        <td><?= $c->qtd ?></td>
        <td><?= $c->total ?></td>
    </tr>
<?php endforeach; ?>

And the ordering should thus be for qtd :

uasort($teste, function ($a, $b) {
    return $a->qtd < $b->qtd;
});

If it is by nome :

uasort($teste, function ($a, $b) {
    return strcmp($a->nome, $b->nome);
});

If you want to get the current index you can do this:

<?php foreach ($curva as $key => $c): ?>
    <tr>
        <td>Índice: <?= $key ?></td>
        <td><?= $c->id ?></td>
        <td><?= $c->nome ?></td>
        <td><?= $c->qtd ?></td>
        <td><?= $c->total ?></td>
    </tr>
<?php endforeach; ?>

If you want to use the index to update you can think about trying to use reference using & ("and" commercial):

  

Doc: link

<?php foreach ($curva as &$c): ?>
    <?php
        $c->nome = 'João';
    ?>
    <tr>
        <td><?= $c->id ?></td>
        <td><?= $c->nome ?></td>
        <td><?= $c->qtd ?></td>
        <td><?= $c->total ?></td>
    </tr>
<?php endforeach; ?>

<?php

print_r($curva); //Veja que o valor foi atualizado

?>
    
07.03.2017 / 18:22