Given a N-bit hash (e.g. output of md5()), I have 2 situations I need solutions for:
- Based on the hash, return an integer value in a given range.
- Based on the hash, return an array value from a given array.
Same hash, should always return same number or array key within that range or from that same input array. If the input array changes but hash remains the same, then i would get a different selection.
So for example i would have code like this:
echo intFromHash(1, 100, 'abcd'); // 15
echo intFromHash(1, 100, 'defg'); // 90
echo arrayValueFromHash(['moe', 'joe', 'pike'], 'abcd'); // 'joe'
echo arrayValueFromHash(['pike', 'dolly']); // pike
intFromHash()in 1 line of code using thecrc32()PHP function. Then you can use it to implementarrayValueFromHash()(in another line of code).