您好,我使用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函数吗? –