-1
I own a Foreach
in an array to mount a given table, the array is ordered, but I need to remove duplicated values and leave only some information of it and not all. My idea is to compare the n
with n-1
where n
is the position of the array, but my doubt is on how to do this with the foreach
, with a for until I know how to solve but with foreach
perhaps from little experience in language I have not found a way.
The code is that way at the moment
foreach ($results as $k => $v) {
$html .= '
<tr>
<td>'.$v->nosso_numero.'</td>
<td>'.$v->ocorrencia.'</td>
<td>R$'.number_format($v->vlr_boleto, 2, ',', '.').'</td>
<td>'.$v->matricula.'</td>
<td>'.$v->nome.'</td>
<td>'.$v->competencia.'</td>
<td>R$'.number_format($v->valor_devido, 2, ',', '.').'</td>
</tr>
';
}
I’d like to make a comparison if
$results[n]->nosso_numero != $results[n-1]->nosso_numero
And assemble based on this check.
Have you tried using
array_unique($array)
?– viana
array_unique
will return me a new array, I need based on my mount.– Guilherme Rigotti
Here’s how you insert some items from your $Results array?
– viana
A question: is this array generated based on a query in the relational database? An alternative, outside of PHP, would be to use a DISTINCT when selecting the data.
– ndr458