Вам нужно сначала перебрать все сообщения и собрать все стили:
$types = [];
$beds = [];
$baths = [];
foreach($posts as $post){
$types = collectKeys("types", $post, $types);
$beds = collectKeys("bedrooms", $post, $beds);
$baths = collectKeys("bathrooms", $post, $baths);
}
function collectKeys($key, $post, $in){
if(!isset($in[$post[$key]])) $in[$post[$key]] = [];
foreach($post["locations"] as $location) $in[$post[$key]][] = $location;
foreach($post["types"] as $type) $in[$post[$key]][] = $type;
return $in;
}
Затем вы можете повторить чекбоксы:
foreach($types as $typekey => $type){
echo "<input type='checkbox' name='types[]' value='$typekey'
class='".implode(" ", $type)."'>";
}
foreach($beds as $bedkey => $bed){
echo "<input type='checkbox' name='bedrooms[]' value='$bedkey'
class='".implode(" ", $bed)."'>";
}
foreach($baths as $bathkey => $bath){
echo "<input type='checkbox' name='bathrooms[]' value='$bathkey'
class='".implode(" ", $bath)."'>";
}