Keeping an array sorted in PHP -


I have a PHP script that reads a large CSV and does some work, but only if the "username" field The unique CSV is used in more than one script, so changing the input from the CPU is not an option to include only unique users.

A very basic program flow (which I am thinking) happens in this way:

  $ allUsernames = array (); While ($ line = fgetcsv ($ fp)) {$ username = $ row [0]; If (in_array ($ username, $ allUsernames) continues; $ All user name [] = $ username; // Process this row}  

Since this CSV can actually be very large, such that in_array bit which I think is the most ideal situation When searching for a member through an array, if it is already sorted, how can you create an array from scratch, keeping it in order ? Once it is in sequence, there will be a more effective way to search for using in_array () , but considering that it is probably not known how the array is sorted it occurs?

Do not keep the array in order, but how about such adaptations? I'm assuming that the array key should be greater than in_array () search.

  $ allUsernames = array (); While ($ line = fgetcsv ($ fp)) {$ username = $ row [0]; If (isset ($ all user name [$ user name])) {continue; } And {$ allUsernames [$ username] = true; // do stuff}}  

Comments