【发布时间】:2018-08-02 07:28:38
【问题描述】:
我有一个 foreach 循环,它根据 ACF 转发器字段 (map_destinations) 的内容执行某些操作。如果有目的地,它可以正常工作,但如果没有,我会得到:
“为 foreach() 提供的参数无效”
这是我的代码:
$map_destinations = get_field('map_destinations', $tour_id);
$map = array();
foreach ($map_destinations as $map_destination) {
$map[] = $map_destination['destination'];
}
我尝试了各种解决方案,但都没有奏效。最普遍接受的方法似乎是:
$map_destinations = get_field('map_destinations', $tour_id);
$map = array();
if (is_array($map)) {
foreach ($map_destinations as $map_destination) {
$map[] = $map_destination['destination'];
}
}
我哪里错了?请多多包涵,我刚刚开始掌握 php。
【问题讨论】:
-
$map_destinations不是数组 :) 您可以使用var_dump($map_destinations)找出它是什么 -
get_field应该返回一个空数组,即使没有可用的数据可以轻松解决
标签: php