簡體   English   中英

如何使用每個組的小時數限制來對一組對象進行分組?

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

我有一個數組:

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
        )

)

我想按“天”對對象數組進行分組/分組。 例如,一個“天”包含10個小時。

所需的輸出:

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] => ...

我怎樣才能做到這一點?

一種方法就是循環

$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

這是一種更簡單/更有效的方法,因為它包含零個函數調用(更重要的是,沒有迭代的函數調用)。 有關過程的解釋,請參見內聯注釋。

代碼:( 演示

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

輸出:

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