I have a foreach loop that does something depending on the contents of an ACF repeater field (map_destinations). It works fine if there are destinations, but if there aren't I get:
"Invalid argument supplied for foreach()"
Here's my code:
$map_destinations = get_field('map_destinations', $tour_id);
$map = array();
foreach ($map_destinations as $map_destination) {
$map[] = $map_destination['destination'];
}
I have tried various solutions but none work. The most commonly accepted method seems to be:
$map_destinations = get_field('map_destinations', $tour_id);
$map = array();
if (is_array($map)) {
foreach ($map_destinations as $map_destination) {
$map[] = $map_destination['destination'];
}
}
Where am I going wrong? Please bear with me, I am just getting to grips with php.