Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中从数组中删除特定值_Php_Codeigniter - Fatal编程技术网

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

在php中从数组中删除特定值,php,codeigniter,Php,Codeigniter,您好,我正在使用Codeigniter构建RESTAPI。问题是,我想知道某一特定房产的价格。我得到的价格是正确的,但我只想得到今年的价格。该表包含2003-2017年的价格,因此我只想显示大于或等于当前年份的价格 所以我有这样的想法: { "status": "success", "status_code": "200", "message": "200 OK", "response": [ { "property_id": "3", "pric

您好,我正在使用Codeigniter构建RESTAPI。问题是,我想知道某一特定房产的价格。我得到的价格是正确的,但我只想得到今年的价格。该表包含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"
    }
{
  "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"
        }
      ]
    }
$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');";
在最底层:

{
      "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": [
    {
      "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"
    }
{
  "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"
        }
      ]
    }
$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');";
如何删除每个对象前面的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));
时间框架_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";
    }

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

您可以使用“查询生成器类”构建查询并以所需的形式检索数据,例如:

$this->db->where(array('Objekt' => $property_id, 'from >='=>'2015-01-01'))->get('prices');
您可以使用“查询生成器类”以所需的形式构建查询并检索数据,例如:

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

我终于这样解决了这个问题:

{
  "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"
    }
{
  "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"
        }
      ]
    }
$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');";

但是,如果能够使用我的_模型使用的内置方法,那就更好了。

我终于成功地解决了这个问题,如下所示:

{
  "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"
    }
{
  "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"
        }
      ]
    }
$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');";

但是,如果能够使用我的模型使用的内置方法,那就更好了。

尝试在DB查询中更改,以根据年份而不是if条件填充结果。请举例说明一下,你能通过函数发布“获取多个”吗?我正在使用此方法,你能尝试在数组中添加一个节点吗('ID'=>$timeframe\u ID,'from>='=>'2015-01-01')。这将根据我认为的年份返回结果。尝试在DB查询中更改,以根据年份而不是if条件填充结果。您可以用示例说明吗?您可以按函数发布get\u many\u吗?我正在使用此函数,您可以尝试在数组中添加节点吗('ID'=>$timeframe\u ID,'from>='=>'2015-01-01')。这将根据我认为的年份返回结果。它没有给我任何信息。请检查我的更新问题,其中我有timeframe\u get方法。它没有返回任何行。表2015中有行,表上没有任何信息。请检查我的更新问题,其中我有timeframe\u get方法。它没有返回任意行。表中有2015行及以上的行