简体   繁体   English

如何使用每个组的小时数限制来对一组对象进行分组?

[英]How to group an array of object using a limit on hours per group?

I have an array: 我有一个数组:

Array
(
    [0] => TspLocation Object
        (
            [latitude] => 32.7308117
            [longitude] => -117.1492819
            [id] => Museum1
            [h] => 2
        )

    [1] => TspLocation Object
        (
            [latitude] => 32.7352917
            [longitude] => -117.1491861
            [id] => Zoo
            [h] => 6
        )

    [2] => TspLocation Object
        (
            [latitude] => 32.72098
            [longitude] => -117.1739938
            [id] => Maritime Museum
            [h] => 2
        )

    [3] => TspLocation Object
        (
            [latitude] => 32.7631797
            [longitude] => -117.2276874
            [id] => Seaworld
            [h] => 6
        )

    [4] => TspLocation Object
        (
            [latitude] => 32.8645458
            [longitude] => -117.2517528
            [id] => Birch
            [h] => 3
        )

    [5] => TspLocation Object
        (
            [latitude] => 32.7700125
            [longitude] => -117.2532622
            [id] => Belmont
            [h] => 4
        )

    [6] => TspLocation Object
        (
            [latitude] => 32.6558527
            [longitude] => -117.1563085
            [id] => Aquatica
            [h] => 4
        )

    [7] => TspLocation Object
        (
            [latitude] => 32.6894411
            [longitude] => -117.1829472
            [id] => Coronado
            [h] => 2
        )

    [8] => TspLocation Object
        (
            [latitude] => 32.7803722
            [longitude] => -117.0442201
            [id] => Lake Murray
            [h] => 2
        )

    [9] => TspLocation Object
        (
            [latitude] => 33.0051734
            [longitude] => -117.016139
            [id] => Poway Lake
            [h] => 2
        )

)

I want to group/chunk the object arrays by "days". 我想按“天”对对象数组进行分组/分组。 One "day" contains 10 hours for example. 例如,一个“天”包含10个小时。

Desired output: 所需的输出:

Array
(
    [1] => Array
        (
            [0] => TspLocation Object
                (
                    [latitude] => 32.7308117
                    [longitude] => -117.1492819
                    [id] => Museum1
                    [h] => 2
                ),
            [1] => TspLocation Object
                (
                    [latitude] => 32.7352917
                    [longitude] => -117.1491861
                    [id] => Zoo
                    [h] => 6
                ),
           [2] => TspLocation Object
                (
                    [latitude] => 32.72098
                    [longitude] => -117.1739938
                    [id] => Maritime Museum
                    [h] => 2
                )
         )
    [2] => Array 
         (
          [0] => TspLocation Object
              (
                  [latitude] => 32.7631797
                  [longitude] => -117.2276874
                  [id] => Seaworld
                  [h] => 6
              )

          [1] => TspLocation Object
              (
                  [latitude] => 32.8645458
                  [longitude] => -117.2517528
                  [id] => Birch
                  [h] => 3
               )
          )
     [3] => ...

How can I do this? 我怎样才能做到这一点?

One way is just by looping 一种方法就是循环

$result = [];                                           // Result array
$group = [];                                            // Group array
foreach ($array as $value) {                            // Loop thru each array
    $subtotal = array_sum(array_column($group, 'h'));   // Get subtotal of group
    if (($subtotal + $value->h) > 10) {                 // check if already exceeds 10
        $result[] = $group;                             // Push group if more than 10 h
        $group = [];                                    // then create new group
    }
    $group[] = $value;                                  // Push value to group
    if (end($array) == $value) {                        // Check if last element of array
        $result[] = $group;                             // Push to group if true
    }
}

print_r($result);                                       // Print result

This is a simpler / more efficient method because it contains zero function calls (more importantly, no iterated function calls). 这是一种更简单/更有效的方法,因为它包含零个函数调用(更重要的是,没有迭代的函数调用)。 See inline comments for explanation of processes. 有关过程的解释,请参见内联注释。

Code: ( Demo ) 代码:( 演示

$array=[
    (object)['latitude'=>32.7308117,'longitude'=>-117.1492819,'id'=>'Museum1','h'=>2],
    (object)['latitude'=>32.7352917,'longitude'=>-117.1491861,'id'=>'Zoo','h'=>6],
    (object)['latitude'=>32.72098,'longitude'=>-117.1739938,'id'=>'Maritime Museum','h'=>2],
    (object)['latitude'=>32.7631797,'longitude'=>-117.2276874,'id'=>'Seaworld','h'=>6],
    (object)['latitude'=>32.8645458,'longitude'=>-117.2517528,'id'=>'Birch','h'=>3],
    (object)['latitude'=>32.7700125,'longitude'=>-117.2532622,'id'=>'Belmont','h'=>4],
    (object)['latitude'=>32.6558527,'longitude'=>-117.1563085,'id'=>'Aquatica','h'=>4],
    (object)['latitude'=>32.6894411,'longitude'=>-117.1829472,'id'=>'Coronado','h'=>2],
    (object)['latitude'=>32.7803722,'longitude'=>-117.0442201,'id'=>'Lake Murray','h'=>2],
    (object)['latitude'=>33.0051734,'longitude'=>-117.016139,'id'=>'Poway Lake','h'=>2]
];
$tally=0;  // init the $tally for arithmetic+assignment operator
foreach($array as $row){
    if(($tally+=$row->h)>10){  // update $tally and check new $tally
        $result[]=$group;      // transfer current $group to $result
        $group=[$row];         // reset $group with current row as first/only element 
        $tally=$row->h;        // reset $tally to current row's h value
    }else{
        $group[]=$row;         // store currrent row in current $group
    }
}
$result[]=$group;              // unconditionally store the final $group to $result
var_export($result);

Output: 输出:

array (
  0 => 
  array (
    0 => 
    stdClass::__set_state(array(
       'latitude' => 32.7308117,
       'longitude' => -117.1492819,
       'id' => 'Museum1',
       'h' => 2,
    )),
    1 => 
    stdClass::__set_state(array(
       'latitude' => 32.7352917,
       'longitude' => -117.1491861,
       'id' => 'Zoo',
       'h' => 6,
    )),
    2 => 
    stdClass::__set_state(array(
       'latitude' => 32.72098,
       'longitude' => -117.1739938,
       'id' => 'Maritime Museum',
       'h' => 2,
    )),
  ),
  1 => 
  array (
    0 => 
    stdClass::__set_state(array(
       'latitude' => 32.7631797,
       'longitude' => -117.2276874,
       'id' => 'Seaworld',
       'h' => 6,
    )),
    1 => 
    stdClass::__set_state(array(
       'latitude' => 32.8645458,
       'longitude' => -117.2517528,
       'id' => 'Birch',
       'h' => 3,
    )),
  ),
  2 => 
  array (
    0 => 
    stdClass::__set_state(array(
       'latitude' => 32.7700125,
       'longitude' => -117.2532622,
       'id' => 'Belmont',
       'h' => 4,
    )),
    1 => 
    stdClass::__set_state(array(
       'latitude' => 32.6558527,
       'longitude' => -117.1563085,
       'id' => 'Aquatica',
       'h' => 4,
    )),
    2 => 
    stdClass::__set_state(array(
       'latitude' => 32.6894411,
       'longitude' => -117.1829472,
       'id' => 'Coronado',
       'h' => 2,
    )),
  ),
  3 => 
  array (
    0 => 
    stdClass::__set_state(array(
       'latitude' => 32.7803722,
       'longitude' => -117.0442201,
       'id' => 'Lake Murray',
       'h' => 2,
    )),
    1 => 
    stdClass::__set_state(array(
       'latitude' => 33.0051734,
       'longitude' => -117.016139,
       'id' => 'Poway Lake',
       'h' => 2,
    )),
  ),
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM