我有一个数组:
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] => ...
我该怎么做?
答案 0 :(得分:2)
一种方法就是循环
$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
答案 1 :(得分:1)
这是一种更简单/更有效的方法,因为它包含零函数调用(更重要的是,没有迭代函数调用)。有关流程的说明,请参阅内联注释。
代码:(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);
输出:
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,
)),
),
)