Sometimes I find that I need to sort some objects by grouping them by several values. I usually do this by concatenating the values ββtogether with an intermediate character or other delimiter, and then use it as the index of the array.
foreach ($objects as $obj) {
$hash = $obj->parent_id . '_' . $obj->date . '_' . $obj->type;
$sorted_objects[$hash][] = $obj;
}
... hic! There should be a better way than abuse of the PHP keyboard and string concatenation. Is there a way to execute a hash for multiple values? It seems I should have just done something like this:
$hash = sha1_multiple($obj->parent-id, $obj->date, $obj->type);
Am I already using the best method, or is there a better way?
source
share