好的,我有一个外卖的结果集,我循环通过,外卖对象有以下属性id,名称,地址,邮政编码,距离,mile_cost,披萨,中文,印度,烤肉。如果他们有比萨饼菜单fr例如,它将包含1或0,如果没有等
我想过滤结果,以便获得最近的比萨饼,中国,印度和烤肉串出售餐厅。但也要过滤结果,这样它就不会显示距离大于10且大于单个外卖最大交付距离的结果。
我必须在php代码中这样做,而不是mysql查询,我有点困惑于如何做到这一点。这是我到目前为止所拥有的
foreach($takeaway as $takeaway){
$distance = $this->geozip->get_distance($location, $takeaway->postcode);//get the distance between the two postcodes
if($distance <= $takeaway->distance && $distance <= 10){
$number = 0;
echo $takeaway->name.' is '.$distance.'miles away <br />';
?>
<div class="takeaway_box">
<div class="takeaway_box_band">Indian</div>
</div>
<?php
}
}
?>
我猜,为了得到每种类型的最近的单个餐厅,我必须找到距离最接近于零的餐厅,然后如何点菜?
任何关于如何做到这一点的帮助都将不胜感激。
发布于 2012-01-20 00:28:37
使用array_filter过滤结果。然后使用usort按距离排序。
在示例中:
function filter_function($takeaway) {
return $takeaway->someProperty == 1; // could be your menu;
}
function sort_function($a, $b) {
// distance must be set before elements are passed trough the sort function
$ad = $a->distance;
$bd = $b->distance;
// sort is ASC
if ($ad == $bd) {
return 0;
}
// todo DESC change the < operator to >
return ($ad < $bd) ? -1 : 1;
}
$takeaways = array_filter($takeaways, filter_function);
usort($takeaways, sort_function);
https://stackoverflow.com/questions/8929368
复制相似问题