您好,我使用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";
}
}
任何想法?先謝謝你!!!
試圖在數據庫查詢中更改以根據年份而不是條件填充結果。 –
可以請你解釋一下嗎? – BRG
你可以發佈get_many_by函數嗎? –