2016-05-19 45 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