选择对象的最小值和最大值

时间:2014-10-06 17:21:25

标签: php object max min

我试图获得最小值的start和最大值的end outoff下面的对象。该对象是sql脚本的结果。

object(CI_DB_mysql_result)[18]
  public 'conn_id' => resource(54, mysql link persistent)
  public 'result_id' => resource(61, mysql result)
  public 'result_array' => 
    array (size=0)
      empty
  public 'result_object' => 
    array (size=3)
      0 => 
        object(stdClass)[16]
          public 'id' => string '601' (length=3)
          public 'scheduled' => string '0' (length=1)
          public 'start' => string '2014-10-17 06:00:00' (length=19)
          public 'end' => string '2014-10-17 11:00:00' (length=19)
      1 => 
        object(stdClass)[19]
          public 'id' => string '602' (length=3)
          public 'scheduled' => string '0' (length=1)
          public 'start' => string '2014-10-17 18:00:00' (length=19)
          public 'end' => string '2014-10-17 19:30:00' (length=19)
      2 => 
        object(stdClass)[20]
          public 'id' => string '603' (length=3)
          public 'scheduled' => string '1' (length=1)
          public 'start' => string '2014-10-17 11:00:00' (length=19)
          public 'end' => string '2014-10-17 18:00:00' (length=19)
  public 'custom_result_object' => 
    array (size=0)
      empty
  public 'current_row' => int 0
  public 'num_rows' => int 3
  public 'row_data' => null

在这种情况下,start =' 2014-10-17 06:00:00'最终的价值= 2014-10-17 19:30:00'所以我正在寻找的结果如下:

$smallest_start_value = '2014-10-17 06:00:00';
$biggest_end_value = '2014-10-17 19:30:00';

为了计算这个结果,代码应该如何?

功能:

function unschedule_resource() {
    $event_id = 4;//$_POST['event_id'];
    $event_start = '2014-10-17 11:00:00';//$_POST['event_start'];
    $event_end = '2014-10-17 18:00:00';//$_POST['event_end'];
    // Get resource id
    $this->db->select('
        event.resource_id'
    );
    $this->db->from('promo_manager.event');
    $this->db->where('event.id =', $event_id);
    $data = $this->db->get();
    foreach ($data->result() as $row) {
        echo $row->resource_id . "<br>";
        $resource_id = $row->resource_id;
    }
    // Get resource events to unschedule
    $this->db->select('
        resource_calendar.id,
        resource_calendar.scheduled,
        resource_calendar.start,
        resource_calendar.end'
    );
    $this->db->from('promo_manager.resource_calendar');
    $this->db->where('resource_calendar.resource_id =', $resource_id);
    $this->db->where('resource_calendar.start =', $event_start);
    $this->db->where('resource_calendar.end =', $event_end);
    $this->db->or_where('resource_calendar.end =', $event_start);
    $this->db->or_where('resource_calendar.start =', $event_end);
    $data = $this->db->get();

    $lowest_value = null;
    $highest_value = null;
    foreach ($data as $the_key => $the_value) {
        if ($the_key == 'start') {
            if ($lowest_value === null) {
                 $lowest_value = $the_value;
            }

            if ($the_value < $lowest_value) {
                 $lowest_value = $the_value;
            }
        }
    }
    //echo $lowest_value;
    var_dump ($lowest_value) ;
}

2 个答案:

答案 0 :(得分:0)

Sam的代码应如下所示:

$lowest_value = null;
$highest_value = null;
foreach($data as $obj) { //$data should be the array containing the result objects.
    $start = $obj->start;
    $end = $obj->end;

    if ($lowest_value === null)
      $lowest_value = $start;
    else
      if ($lowest_value > $start)
        $lowest_value = $start;

    if ($highest_value === null)
      $highest_value = $end;
    else
      if ($highest_value < $end)
        $highest_value = $end;
 } 

可缩短为:

    if ($lowest_value === null || $lowest_value > $start)
      $lowest_value = $start;

    if ($highest_value === null || $highest_value < $end)
      $highest_value = $end;

两个值都可以在一次迭代中生成而没有问题。请注意,<>适用于您将日期格式化为给定的情况。另外,您需要使用日期功能来比较日期。

您还可以使用MySQL使用聚合方法Min或Max直接查询Min / Max值:http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html

(如果您不需要加载每个对象,这将更快)

答案 1 :(得分:-1)

我不确定在选择声明中你是否有办法做到这一点,我不会这样做。将结果对象放入数组中。这是超级基础,但应该完成工作。

$lowest_value = null;
$highest_value = null;
foreach(result_object as $the_key => $the_value) {
    if($the_key == 'start') {
        if($lowest_value === null) {
             $lowest_value = $the_value;
        }

        if(the_value < $lowest_value) {
             $lowest_value = $the_value;
        }
    }
} 

$lowest_value = null; $highest_value = null; foreach(result_object as $the_key => $the_value) { if($the_key == 'start') { if($lowest_value === null) { $lowest_value = $the_value; } if(the_value < $lowest_value) { $lowest_value = $the_value; } } }

只需更改操作符

再次检查结束值