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.
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,
)),
),
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.