Greetings There was a problem. There is an array of array ()
[0] => 123 [1] => orange [2] => apple [3] ...

 и в нем например 50000 значений. Нужно найти в нем значение Apple и вернуть его ключ. 

Do functions like in_array just return the key? I tried the array_search function but it passes as I understand it through the entire array and takes a lot of time, and in_array as I understand it checks and takes much less time. So how to do what Apple would find and return its key?

  • You can simply write a passage through the array and compare the value with the "apple", end the loop when the value is found. - Apemother
  • Not so simple, it will take a lot of time I did that before. If, for example, you already need to find an apple first and then orange, then it will run the entire array on the apple and then on orange, and this takes a lot of time, and if you immediately call something like in_array, it would turn out that it checks if the key exists and takes does not waste time on its search. - Bogdan
  • anyway, in_array goes through the entire array as well and searches. You can write a condition that will combine the desired values - Apemother
  • one
    @ Bogdan, test gist.github.com/ksimka/21a6ff74b41451c430e8 and a comment there at the bottom. - Visman
  • @Visman, Thank you, very much like what I need - Bogdan

1 answer 1

"function of type in_array only to return the key" - this is array_search ()

However, it is not necessary to imagine that the built-in function will perform the search in some special magical way, and do it faster than brute force in a loop, written by hand. These functions will be searched in the same way, that for an array of this size it will be very inefficient.

therefore, first of all, you should try to avoid such a search altogether. For example, writing data to the database, and making search requests already to it.

Or by representing an array in another form, for example, by writing values ​​as keys to another array. This will work if there are no duplicate values ​​in the array.

In any case, the specific solution depends on the details of the specific task.

  • one
    Functions of the standard PHP library, incl. and array_search () is faster than manually written iterations, since they are executed at a low level. Comparison of the use of functions array_search, in_array and technology with replacement of keys with values ​​can be studied here: stackoverflow.com/questions/4518404/… - Dmitry Nevzorov
  • 1. No need to retell stories about the "low level". non-optimal algorithm at any level will remain non-optimal. 2. Under the link about manual search there is nothing. 3. even if it were, the monstrous ways of measuring the performance used by the unfortunate pokapeshnikami in any case have nothing to do with real life - Ipatyev
  • If the algorithm requires searching for a value in the array, it means that this is necessary from the point of view of business logic. Your proposed alternative with keys violates the program logic, being a controversial crutch + the uniqueness of the values ​​may not be guaranteed. The alternative to writing to the database is a complete violation of the logic of segregation of duties + a performance crash, because interaction with the database and subsequent data processing is not cheaper than searching the array. - Dmitry Nevzorov
  • @DmitryNevzorov all these idle arguments are complete bullshit, if they are used to justify a search in an array of 50k records. - Ipatiev