2012-01-19 133 views
1

好吧我有一個結果集的外賣,我循環,外賣對象有以下屬性ID,名稱,地址,郵編,距離,mile_cost,披薩,中國,印度,烤肉串。如果他們有一個比薩菜單fr exampe它將包含1或0,如果不是等PHP訂購結果的代碼邏輯

我想過濾結果,以獲得最接近的單比薩餅,中國,印度和烤肉串賣餐館。但是也要篩選結果,以便它不會顯示距離大於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 
     } 
    } 
    ?> 

我想,以獲得每種類型的最接近的單餐廳我會找到那些最接近的距離爲零,然後命令他們一些如何?

任何幫助如何做到這一點將不勝感激。

回答

0

使用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);