在php中从数组中删除特定值

时间:2016-05-19 12:16:27

标签: php codeigniter

您好我正在使用Codeigniter构建REST API。问题是我想得到特定房产的价格。我得到了正确的价格,但我想得到当年的价格。该表的价格从2003年到2017年,所以我只想显示大于或等于当年的价格。

所以我有这样的事情:

{
  "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";
    }

 }

有什么想法吗?提前谢谢!!!

2 个答案:

答案 0 :(得分:2)

您可以使用"查询构建器类"构建查询并以您想要的形式检索数据,如:

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

答案 1 :(得分: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使用的内置方法会更好。