A
A
Anton2016-09-29 16:12:39
PHP
Anton, 2016-09-29 16:12:39

How to find a number in a large array?

Hello. There is an array, for example, for 1000 elements, all numbers are different, but one matches with whatever, how best to search for this element?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
G
Grigory Esin, 2016-09-29
@xotey83

Or even simpler: in_array
If you need to search several times in the same array, then it's better to first make it an array_flip, and then use isset

A
Alexey, 2016-09-29
@alsopub

As an option:

$a = [ 1, 2, 3, 3, 4, 5 ];
$n = array_search(2, array_count_values($a));
echo($n);

Result: 3

V
Vladimir Olohtonov, 2016-09-29
@sgjurano

If the numbers are in order, then using the sum of an arithmetic progression.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question