Skip to content

Instantly share code, notes, and snippets.

@lighty
Created March 5, 2014 15:21
Show Gist options
  • Save lighty/9369314 to your computer and use it in GitHub Desktop.
Save lighty/9369314 to your computer and use it in GitHub Desktop.
指定したキーで配列をグルーピングする
class KeyGrouper{
public static function group($records, $keys){
$groupedRecords = array();
foreach($records as $record){
$primaryKeyString = KeyGrouper::primaryKeyString($record, $keys);
$groupedRecords[$primaryKeyString][] = $record;
}
return $groupedRecords;
}
private static function primaryKeyString($record, $keys){
$str = array();
foreach($keys as $key){
$str[] = "{$key}:{$record[$key]}";
}
return implode("_", $str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment