Answer the question
In order to leave comments, you need to log in
How to group similar array elements?
Hello, there is an array of values that are sorted by the key order , you need to go through the array and display the values in a div . ( data is taken from the database and already sorted! )
$items= array(
array("order" => 1),
array("order" => 2),
array("order" => 2),
array("order" => 3),
array("order" => 4)
);
--- multidimensional array, typo, thanks 27cm <div>1</div>
<div>22</div>
<div>3</div>
<div>4</div>
Answer the question
In order to leave comments, you need to log in
Somehow I did it myself:
$last_order = -1;
foreach($items as $item){
if($last_order != -1 && $item['order'] != $last_order)
{
echo '</div>';
}
if($item['order'] != $last_order)
{
echo '<div>';
$last_order = $item['order'];
}
echo $item['order'];
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question