0

The in_array function is very slow for large arrays due to doing a linear search.

A faster alternative is to search the key of the array.

Thus

if (isset($array[$val]))

is much faster than

if (in_array($val,$array))

for large arrays. However using unicode as array keys will not work.

Is there an alternative way to do this for unicode without resorting to linear searches such as in_array or array_search or generating hashes like md5?

user813801
  • 521
  • 2
  • 6
  • 23

1 Answers1

2

You can use anything as key which can be converted into a string.

Compare: Characters allowed in php array keys?

But nevertheless apparently some poeple have problems with special characters in their array-keys. I bet this may be the case if you use different encodings at the time you store the key and when you search for it. For example your keys come from a database using UTF-8, but when you search you have the key you search for hardcoded in a Iso-encoded PHP-script. This is just an example, there are dozens of scenarios like this. To ensure you always use the same encoding I would use rawurlencode.

Paflow
  • 2,030
  • 3
  • 30
  • 50