I have an element with title, type, description and so on. I have a DAO that defines all that and functions already created that extract the type for one element only: it’s getById().
How can I loop through all the elements and create groups of them by the same id?
$group = array();
foreach ($announcement as $announcements) {
$announcementType = $this->getById($this->typeId());
}
I should put an "if" here, but I cannot understand how to "create a group for each elements with the same typeId".
>Solution :
It’s not clear why you cycle over $announcements, anyway you can try a solution like this:
$group = [];
foreach ($announcements as $announcement) {
$id = $this->typeId();
$group[$id][] = $this->getById($id);
}