Hash method for multiple values?

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.

// group all objects with a common parent_id, date, and type
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?

+3
source share
2 answers

- , , , , , - , .

+2

PHP , :

function sha1_multiple() {
    $args = func_get_args();
    return sha1(serialize($args));
}
+5

All Articles