I have a big array that contains string values as the data. I want to optimize this array, so I can perform queries that check if a certain string exists in the array as quickly as possible. So let's say I create the array with $arr= [];
and then add values like this:
foreach($names as $name)
$arr[]= $name;
And now I want to perform a lot of queries like if(in_array($random_string, $arr))
, but it's pretty slow. I'd like to add some indexing for the array to optimize the performance. Should I simply use the sort()
function for the array?
How to optimize an array with string data for checking if a string exists queries?
EDIT: No, obviously this is not a duplicate of "what is faster: in_array or isset? [closed]", and you can see that already by the answer by vivek_23.