【发布时间】:2015-09-21 19:58:33
【问题描述】:
我有一个 foreach 循环,它遍历帖子并执行操作(例如为每个帖子设置一个 $distance 变量)。通过条件,它需要做两件事,它们独立工作,但我无法让它们一起工作。
$results[] = $value; 有效,因为它添加了数组 ($value)
$results['distance'] = $distance; 自己工作,但我需要包含 $value 数组。
如果我把它们都放进去,它会产生两倍于应有的数组。距离应该包含在值中。如果我执行array_push 也可以,但我需要指定密钥。
foreach ($posts as $key => $value) {
$loop->the_post();
$result_lat = get_post_meta( $value->ID, 'latitude', true );
$result_long = get_post_meta( $value->ID, 'longitude', true );
$distance = round(calc_distance($input_lat, $input_lng, $result_lat, $result_long, "M"));
// add item to results if within distance
if ($distance < $_GET['within']) {
$results[] = $value;
$results['distance'] = $distance; // add distance to array
}
}
【问题讨论】:
标签: php arrays wordpress loops foreach