Created
March 5, 2014 15:21
-
-
Save lighty/9369314 to your computer and use it in GitHub Desktop.
指定したキーで配列をグルーピングする
This file contains 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
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