Converting a string to a number consistently using PHP

Say you want to convert strings to numbers such that a string will always convert to the same number. So for example the string
“red” will always convert to 2 or the string “blue” will always convert to 1. You don’t know which strings you are going to get and the exact number they convert to doesn’t matter just that it always converts to the same number. The reason you want to do this is to pick a value from an array based on that string-converted-to-a-number i.e. you want to use the string-converted-to-a-number as the array’s index or key. How do you do that conversion using PHP?
Continue reading