Last active
May 31, 2023 00:07
-
-
Save bor0/b5f449bfe85440d96abd933b9f03b310 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$array = [ | |
[ 'id' => 1, 'value' => 'foo' ], | |
[ 'id' => 1, 'value' => 'bar' ], | |
[ 'id' => 2, 'value' => 'baz' ], | |
]; | |
$groups = array_group( $array, function( $a, $b ) { | |
return $a['id'] == $b['id']; | |
} ); | |
var_dump( $groups ); | |
/* | |
array(2) { | |
[0]=> | |
array(2) { | |
[0]=> | |
array(2) { | |
["id"]=> | |
int(1) | |
["value"]=> | |
string(3) "foo" | |
} | |
[1]=> | |
array(2) { | |
["id"]=> | |
int(1) | |
["value"]=> | |
string(3) "bar" | |
} | |
} | |
[1]=> | |
array(1) { | |
[0]=> | |
array(2) { | |
["id"]=> | |
int(2) | |
["value"]=> | |
string(3) "baz" | |
} | |
} | |
} | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function less_than_or_equal( $a, $b ) { | |
return $a <= $b; | |
} | |
$arr1 = array(1,2,2,3,1,2,0,4,5,2); | |
var_dump( array_group($arr1, 'less_than_or_equal') ); | |
/* | |
array(4) { | |
[0]=> | |
array(4) { | |
[0]=> | |
int(1) | |
[1]=> | |
int(2) | |
[2]=> | |
int(2) | |
[3]=> | |
int(3) | |
} | |
[1]=> | |
array(2) { | |
[0]=> | |
int(1) | |
[1]=> | |
int(2) | |
} | |
[2]=> | |
array(3) { | |
[0]=> | |
int(0) | |
[1]=> | |
int(4) | |
[2]=> | |
int(5) | |
} | |
[3]=> | |
array(1) { | |
[0]=> | |
int(2) | |
} | |
} | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function event( $start, $end ) { | |
return (object)array( 'start' => strtotime( $start ), 'end' => strtotime( $end ) ); | |
} | |
function overlaps( $ev1, $ev2 ) { | |
$start_time1 = $ev1->start; | |
$start_time2 = $ev2->start; | |
$end_time1 = $ev1->end; | |
$end_time2 = $ev2->end; | |
return ($start_time1 <= $end_time2 && $start_time2 <= $end_time1); | |
} | |
$events = [ | |
event( '2023-05-01 00:00:00', '2023-05-01 01:00:00' ), | |
event( '2023-05-01 00:30:00', '2023-05-01 01:15:00' ), | |
event( '2023-05-01 02:00:00', '2023-05-01 03:00:00' ), | |
]; | |
$grouped = array_group( $events, 'overlaps' ); | |
var_dump( $grouped ); // produces [ [ $events[0], $events[1] ], [ $events[2] ] ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$array = [ | |
[ 'id' => 1, 'value' => 'foo' ], | |
[ 'id' => 1, 'value' => 'bar' ], | |
[ 'id' => 2, 'value' => 'baz' ], | |
]; | |
$groups = []; | |
foreach ( $array as $element ) { | |
$groups[ $element['id'] ][] = $element; | |
} | |
var_dump( $groups ); | |
/* | |
array(2) { | |
[1]=> | |
array(2) { | |
[0]=> | |
array(2) { | |
["id"]=> | |
int(1) | |
["value"]=> | |
string(3) "foo" | |
} | |
[1]=> | |
array(2) { | |
["id"]=> | |
int(1) | |
["value"]=> | |
string(3) "bar" | |
} | |
} | |
[2]=> | |
array(1) { | |
[0]=> | |
array(2) { | |
["id"]=> | |
int(2) | |
["value"]=> | |
string(3) "baz" | |
} | |
} | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment