0
I have a array
in PHP that follows the following pattern:
array(3) {
[0]=> array(3) {
["id"]=> string(1) "1"
["categoria"]=> string(7) "Celular"
["produto"]=> string(8) "Motorola"
}
[1]=> array(3) {
["id"]=> string(1) "2"
["categoria"]=> string(7) "Celular"
["produto"]=> string(6) "Iphone"
}
[3]=> array(3) {
["id"]=> string(1) "3"
["categoria"]=> string(9) "Acessório"
["produto"]=> string(10) "Carregador"
}
}
If I do a foreach, they will all come in order, but I would like to group by the item "category", to return the following:
<div>
<h1>Celular</h1>
<ul>
<li>Motorola</li>
<li>Iphone</li>
</ul>
</div>
<div>
<h1>Acessório</h1>
<ul>
<li>Carregador</li>
</ul>
</div>
I tried to make a foreach with if
, but as it is a dynamic array, the if
doesn’t solve forever