Group values from a multidimensional array with the same value and display in a table

Asked

Viewed 212 times

-1

Good morning,

I have a multidimensional array, shown below:

    array(3) {
    [0]=>
    array(1) {
        [0]=>
        object(stdClass)#35 (3) {
        ["id"]=>
        string(2) "73"
        ["valor"]=>
        string(6) "200.00"
        ["compra_nome"]=>
        string(8) "Chaveiro"
        }
    }
    [1]=>
    array(3) {
        [0]=>
        object(stdClass)#36 (3) {
        ["id"]=>
        string(2) "74"
        ["valor"]=>
        string(6) "500.00"
        ["compra_nome"]=>
        string(17) "Pulseira"
        }
        [1]=>
        object(stdClass)#37 (3) {
        ["id"]=>
        string(2) "74"
        ["valor"]=>
        string(6) "200.00"
        ["compra_nome"]=>
        string(14) "Chave de Fenda"
        }
        [2]=>
        object(stdClass)#38 (3) {
        ["id"]=>
        string(2) "74"
        ["valor"]=>
        string(6) "100.00"
        ["compra_nome"]=>
        string(15) "Chuveiro"
        }
    }
    [2]=>
    array(1) {
        [0]=>
        object(stdClass)#39 (3) {
        ["id"]=>
        string(2) "75"
        ["valor"]=>
        string(6) "100.00"
        ["compra_nome"]=>
        string(14) "Lampada"
        }
    }
    }

I need to store in an array the values that the id, so I would need to do a foreach to mount a table with the values, so it should look like this:

inserir a descrição da imagem aqui

My PHP code:

$clientes = $this->Oportunidades_model->buscaClientesComCondicoes($data);
$clientes_array =  array();
        foreach ($clientes as $key => $value) {
            // var_dump($value->codigo_cli);
            $clientes_array[] = $this->Oportunidades->buscaCompras($value->codigo_cli,$data);
        }

PS: The variable that brings the multidimensional array at the beginning of the question is $clientes_array No ideas ;/

I’m using PHP with Codeigniter but can be pure php too..

Edit: is returning with its code:

inserir a descrição da imagem aqui

It would need to return 3 tables and join the 73.74.75 records in 3 different tables equal to the image above;

1 answer

0

Friend your initial array already has the list sorted by the ids, you just need to scroll through it and mount the table.

<table>
    <thead><th>ID</th><th>VALOR</th><th>COMPRA NOME</th></thead>
    <tbody>
        <?php foreach($clientes_array  as $items):?>
            <?php foreach($items as $item):?>
        <tr>
            <td><?php print $item->id; ?></td>
            <td><?php print $item->valor; ?></td>
            <td><?php print $item->compra_nome?></td>
        </tr>
            <?php endforeach;?>
        <?php endforeach;?>
    </tbody>
</table>

If you want to group the records by value:

$lista = [];

foreach ($clientes_array as $items) {
    foreach ($items as $item) {
        if (!isset($lista[$item->valor]))
        {
            $lista[$item->valor] = [];
        }

        $lista[$item->valor][] = $item;
    }
}
<table>
    <thead><th>ID</th><th>VALOR</th><th>COMPRA NOME</th></thead>
    <tbody>
        <?php foreach($lista as $items):?>
            <?php foreach($items as $item):?>
        <tr>
            <td><?php print $item->id; ?></td>
            <td><?php print $item->valor; ?></td>
            <td><?php print $item->compra_nome?></td>
        </tr>
            <?php endforeach;?>
        <?php endforeach;?>
    </tbody>
</table>
  • Thank you for your Vinicius respostsas! I put an Edit in the post showing how it is getting. Are 3 different ids and 6 tables. I would need it to be 3 tables with the respective ids, and the records that are of the same ID would be in the same table. In other words, table id 74(has 3 records) ai in this table will have the 3 records of ID 74.

Browser other questions tagged

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