【问题标题】:Select smallest and biggest value of an object选择对象的最小值和最大值
【发布时间】:2014-12-01 00:26:59
【问题描述】:

我试图从下面的对象中获取 start 的最小值和 end 的最大值。该对象是 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',end 的最大值 = '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) ;
}

【问题讨论】:

    标签: php object max min


    【解决方案1】:

    我不确定是否有一种方法不能在 select 语句中执行此操作,我假设不会。将您的结果对象放入一个数组中。这是超级基本的,但应该可以完成工作。

    $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;
            }
        }
    } 
    

    只更改运算符,再次检查最终值

    【讨论】:

    • 嗨,Sam,感谢您的解决方案。不幸的是,当我执行 var_dump 时,我得到了 NULL 值。当我回显该值时,我看不到任何结果。我做错了什么?请参阅上面的完整功能...
    • 你只是在迭代一个对象的属性。如果result_object 应该指向包含对象的数组,$the_key 将始终是数字,因此您对“开始”的比较失败。
    【解决方案2】:

    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;
    

    这两个值都可以在一次迭代中生成而不会出现问题。请记住,&lt;&gt; 在您保持给定日期格式的情况下有效。否则,您将需要使用日期功能来比较日期。

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

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

    【讨论】:

    • 你好 Dognose,代码仍然没有给我任何价值,但我确实得到了正确的最小值和最大值,所以谢谢提示......
    • @user3739279 np。代码的逻辑看起来很好(它未经测试的ofc。)因此,唯一可能与此代码不匹配的是输入$data - 或者它是空的,它将ofc。将所有值保持为 null。
    猜你喜欢
    • 2019-02-09
    • 2020-08-19
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 2021-08-14
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    相关资源
    最近更新 更多