Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency of Searching an Array Vs Searching in Text.... Which is Better?

I have a List of (integer)ID's, which i am storing as text, like

23;45;67;12;332;783;123;33;15;87;41;422;88;58;

now i am working with PHP, i want to check if a particular ID already exists in that TEXT, i have the explode function, which can give me an array of numbers, and then i can use the in_array function, alternatively i can just use the strpos function to find in text.

so which one will be more efficient accourding to you ?

Thanks a lot for taking time to read this.

like image 339
Pheonix Avatar asked Feb 23 '23 21:02

Pheonix


1 Answers

If all you have to do is look up a single ID, then strpos() will be more efficient, because all it has to do is find an occurrence of id;, whereas explode() will do a lot more than that, not to mention the costly call to in_array().

like image 139
rid Avatar answered Apr 27 '23 03:04

rid