how to echo the array using foreach

Asked

Viewed 720 times

0

Well I have a variable $curva that I counted a array, if I’m 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 so:

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’s not working. Someone knows what it might be?

  • 3

    <td><?= $c['id'] ?></td> works?

  • mayor worked out right here, thank you

1 answer

4


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

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

A tip for when mixing html and php, but it’s just my opinion, is to use the alternative syntax this way (but only when mixing HTML and PHP, of course it will matter of taste too):

<?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 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 it like this:

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 ordination should stay so be by qtd:

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

If it’s for nome:

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

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

<?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 Indice to update you can think about trying to use reference using the & ("and "commercial):

Doc: http://php.net/manual/en/language.references.php

<?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

?>
  • 2

    mayor worked out right here, thank you

  • 1

    Cool, thanks.

  • Just one more debt, how do I recover the index? I need to update a specific field.

  • The índice. To upgrade I do so $curva[0]['nome'] = 'CD de Musica';I need to know the index so I can update the right field. How I get it from the variable $c?

  • I want to update an array field, and I need to capture its index. Got it?

  • but that’s right, I want to do inside the foreach how do I pick the indices? In the example I put with dump I have 5 indices.

  • tried so foreach ($curva as $c) {&#xA; &#xA; echo key($c);&#xA;&#xA;}didn’t work out.

  • @Hugoborges updated the answer, see if he understood how to get the key (Dice) or how to use references.

  • 1

    That’s right, vlw by Patience hahahah.

Show 4 more comments

Browser other questions tagged

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