2016-05-19 108 views
0

您好,我使用Codeigniter構建了一個REST API。問題是我想要獲得特定房產的價格。我正確地得到價格,但我只想得到當年的價格。該表的價格從2003年至2017年,所以我只想顯示價格大於或等於當年。在php中刪除數組中的特定值

所以我有這樣的事情:

{ 
    "status": "success", 
    "status_code": "200", 
    "message": "200 OK", 
    "response": [ 
    { 
     "property_id": "3", 
     "price_id": "66", 
     "price": "350", 
     "currency": "EUR", 
     "timeframe_id": "1" 
    }, 
    { 
     "property_id": "3", 
     "price_id": "70", 
     "price": "300", 
     "currency": "EUR", 
     "timeframe_id": "6" 
    } 

,並在最底層:

{ 
     "property_id": "3", 
     "price_id": "167547", 
     "price": "500", 
     "currency": "EUR", 
     "timeframe_id": "1186", 
     "periods": [ 
     { 
      "from": "2015-12-12", 
      "to": "2015-12-19", 
      "day": "Sa" 
     } 
     ] 
    }, 
    { 
     "property_id": "3", 
     "price_id": "167548", 
     "price": "550", 
     "currency": "EUR", 
     "timeframe_id": "1187", 
     "periods": [ 
     { 
      "from": "2015-12-19", 
      "to": "2015-12-26", 
      "day": "Sa" 
     } 
     ] 
    } 
    ] 
} 

我想要做的是隻顯示他們有時間的價格是什麼。所以我用unset但結果都在這樣一個奇怪的方式:

{ 
    "status": "success", 
    "status_code": "200", 
    "message": "200 OK", 
    "response": { 
    "582": { 
     "property_id": "3", 
     "price_id": "167498", 
     "price": "300", 
     "currency": "EUR", 
     "timeframe_id": "1137", 
     "periods": [ 
     { 
      "from": "2015-01-03", 
      "to": "2015-01-10", 
      "day": "Sa" 
     } 
     ] 
    }, 
    "583": { 
     "property_id": "3", 
     "price_id": "167499", 
     "price": "300", 
     "currency": "EUR", 
     "timeframe_id": "1138", 
     "periods": [ 
     { 
      "from": "2015-01-10", 
      "to": "2015-01-17", 
      "day": "Sa" 
     } 
     ] 
    } 

如何刪除這個582:{中的每個對象的前面?我的代碼是:

$prices = $this->Model_prices->get_many_by(array('Objekt' => $property_id)); 
      foreach ($prices as $key => $value) { 
       $data = $this->timeframe_get($value['timeframe_id']); 
       foreach ($data as $k => $v) { 
        $from = $v['from']; 
        if (date("Y", strtotime(".$from.")) >= "2015") { 
         $prices[$key]['periods'] = $data; 
        }else{ 
         unset($prices[$key]); 

        } 

       } 


      } 
      $this->response(array('status' => 'success', 'status_code' => '200', 'message' => '200 OK', 'response' => $prices)); 

的timeframe_get方法:

public function timeframe_get($timeframe_id){ 
    $this->load->model('Model_timeframe'); 
    $this->load->database(); 
    // $sql = "SELECT ID as id, von as _from, bis as _to FROM zeitraeumevk WHERE ID = $timeframe_id AND YEAR(von) >= YEAR('2015-01-01')"; 
    // $query = $this->db->query($sql); 
    $timeframes = $this->Model_timeframe->get_many_by(array('ID' => $timeframe_id)); 
    if ($timeframes) { 
     return $timeframes; 
    } else { 
     return "There is no timeframe specified for this property"; 
    } 

} 

任何想法?先謝謝你!!!

+1

試圖在數據庫查詢中更改以根據年份而不是條件填充結果。 –

+0

可以請你解釋一下嗎? – BRG

+0

你可以發佈get_many_by函數嗎? –

回答

0

我終於設法解決這個問題是這樣的:

$sql = "SELECT z.ID as id, z.von as _from, z.bis as _to, p.ID as price_id, p.Objekt as property_id, p.Preis as price FROM timeframe` z INNER JOIN prices p ON z.ID = p.Zeitraum INNER JOIN properties o ON p.Objekt = o.ID WHERE p.Objekt = ".$property_id." AND YEAR(z.von) >= YEAR('2015-01-01');"; 

但實際上可以使用MY_Model使用的內置方法會更好。

2

您可以使用「查詢生成器類」,以構建查詢和檢索要在表單中的數據,是這樣的:

$this->db->where(array('Objekt' => $property_id, 'from >='=>'2015-01-01'))->get('prices'); 
+0

它不給我任何東西。請檢查我有更新的問題,其中我有timeframe_get方法。 – BRG

+0

哎呀,我的壞嘗試現在使用它.... –

+0

它不返回任何行。並且表格2015年內還有一些行 – BRG