I have a foreach loop where I go through a collection of contents and I am making an associative array with values from a relationship of contents. This is the code:
$contentTaxonomies = [];
foreach($contents as $content) {
foreach($content->taxonomies as $taxonomy){
$contentTaxonomies[$content->id][$taxonomy->type->name] = $taxonomy->name;
}
}
But, that means if the content doesn't have any taxonomies it won't be in the $contentTaxonomies array, how can I make a loop that when it doesn't have taxonomies, it still gets add to an array just with empty value?
contentwill not havetaxonomiesthen the innerforeachwill give error?