manipulating an array to mount the grid

Asked

Viewed 54 times

0

I have an array in php and I need to group the values to be able to place them in a table>phtml

Segue jsonArray You’ll always have these five types of transport.

I couldn’t do much to leave here what’s already been done, the only thing I have is

public function relatorio($dtInicio, $dtFinal) {
        $dbControleVeiculos = new \Db\Fisc\ControleVeiculos();
        $original = iterator_to_array($dbControleVeiculos->getResumoControleVeiculos($dtInicio, $dtFinal));

        $result = array();
        foreach ($original as $data) {
            $id = $data['nome'];
            if (isset($result[$id])) {
                $result[$id][] = $data;
            } else {
                $result[$id] = array($data);
            }
        }

        return $result;
        // return 
    }

My goal is to keep it that way.

inserir a descrição da imagem aqui

1 answer

0

If it’s the sum index, you could do so:

if (isset($result[$id])) {
     $result[$id][] = $data;
     $result[$id]['sum_total] += $data['sum']
} else {
     $result[$id] = array($data);
     $result[$id]['sum_total] += $data['sum']
}

But it would be more organized if you gave groupBy by name. Then you could do the most complete foreach +/- like this:

foreach($original as $nome => $data) {...}

Where that name would be an index with the value of its name in question

Browser other questions tagged

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