通过多个条件过滤二维数组(查询结果集)

问题描述 投票:0回答:3

有没有任何解决方案可以在查询中进行搜索。

示例:

我有一个简单的查询:

$this->db->where('start >=',time());
$this->db->where('end <=',strtotime("+1 month"));
$result = $this->db->get('bookings');

然后我希望它能够进行类似于我上面的查询的搜索,但它不在数据库中搜索,而是在 $result 结果中搜索。

所以应该可以做类似的事情:

$where = array(
  'start >=' => time(),
  'end <=' => strtotime("+1 day")
);
$get_result_info_by_search = $this->Search_model->get_stored_results($result, $where);

目标是减少对数据库的调用次数,因为我的间隔始终相同(我循环遍历日期范围并进行相同的调用,例如 31 次(如果是 31 天)

php arrays filtering
3个回答
1
投票

这可以通过循环行并测试“开始”和“结束”值来轻松完成。

/**
 * @param CI_DB_result instance $result
 * @param array $where with two keys, 'start' and 'end', containing timestamp values, ie. 
 * $where = ['start' => time(), 'end' => strtotime("+1 month"));
 * @return mixed An array of db row objects or NULL if nothing matches
 */
public function get_stored_results($result, $where)
{
    $rows = $result->result();
    foreach ($rows as $row)
    {
        if($row->start >= $where['start'] && $row->end <= $where['end'])
        {
            $matches[] = $row;
        }
    }
    return isset($matches) ? $matches : NULL;
}

如果您想取回行数组的数组

public function get_stored_results($result, $where)
{
    $rows = $result->result_array();
    foreach ($rows as $row)
    {
        if($row['start'] >= $where['start'] && $row['end'] <= $where['end'])
        {
            $matches[] = $row;
        }
    }
    return isset($matches) ? $matches : NULL;
}

0
投票

也许会对你有帮助 在你的控制器中

$get_result_info_by_search = $this->Search_model->get_stored_results('start','end',$starttime,$endtime);

在您的搜索模型中

function get_stored_results($where1,$where2,$value1,$value2)

`{


0
投票

也许它会在你的模型中为你工作,尝试添加此代码

function get_stored_results($where)
{
   foreach($where as $key => $value)
   {
     $this->db->where($key,$value);
   }
   $result = $this->db->get('bookings');
}
© www.soinside.com 2019 - 2024. All rights reserved.