[urgent] [php] arrays [modified]
-
hi all, i'm using php and i have a large array (it has several thousand entries). i need to search the array, several thousand times, for a value. the in_array() function works, but the page takes a while to be generated. is their a faster function or way to find out if an element exists in an array? thanks in advance. -- modified at 0:52 Monday 21st August, 2006
-
hi all, i'm using php and i have a large array (it has several thousand entries). i need to search the array, several thousand times, for a value. the in_array() function works, but the page takes a while to be generated. is their a faster function or way to find out if an element exists in an array? thanks in advance. -- modified at 0:52 Monday 21st August, 2006
Make sure that the values are sorted in the array, so that you can use a binary search[^]. That would significantly speed up the process. Looping to find a value in an array of 10000 values would need an average of 5000 compares, while binary search will use around 14 compares. That would mean that the process would be around 350 times faster.
--- b { font-weight: normal; }