Assume that the array has 10000 elements, and the key value is an unordered positive integer smaller than 1000000, and is not continuous, as shown in the following {code ...} now I want to obtain the elements whose key value is greater than 500 and less than 600 in the array $ arr. Is there a more efficient algorithm without having to completely loop through foreach? Assume that the array has 10000 elements, and the key value is an unordered positive integer smaller than 1000000 and is not continuous, as shown below:
$arr=array(1=>'a',20=>'ad',5002=>'ss',190023=>'sd',248=>'ff',76=>'sddd'...);
Now you need to obtain elements whose key value is greater than 500 and less than 600 in the array $ arr.ForeachIs there a more efficient algorithm for completely repeating it?
Reply content:
Assume that the array has 10000 elements, and the key value is an unordered positive integer smaller than 1000000 and is not continuous, as shown below:
$arr=array(1=>'a',20=>'ad',5002=>'ss',190023=>'sd',248=>'ff',76=>'sddd'...);
Now you need to obtain elements whose key value is greater than 500 and less than 600 in the array $ arr.ForeachIs there a more efficient algorithm for completely repeating it?
$ Res = array ();
For (I = 501; I <600; I ++ ){
If (! Isset ($ arr [$ I]) continue;
$ Res [] = $ arr [$ I];
}
@
Php's built-in sorting sort is fast sorting, and the time complexity is O (nlogn). Then you can pick it out with a half-fold choice or something.
The overall time complexity is O (nlogn)
If the time complexity of traversal is O (n), we need to query the values of the I segments. The time complexity is O (I * n), and I is relatively large, it is almost O (n ^ 2), but the actual situation should be I far less than n, and the time complexity is about O (n)
In addition, if you sort data first and need copies, the memory usage will be higher.
So we still have to handle it.
@ Zhou Xiang: I tested it. array_walk () takes a longer time than foreach. It also calls the same user-defined function.
walk_test.php
foreach_test.php
Root @ debian :~ /Coding/php/test # time php foreach_test.php
Real 0m2. 286 s
User 0m1. 088 s
Sys 0m1. 156 s
Root @ debian :~ /Coding/php/test # time php pai_test.php
Real 0m2. 653 s
User 0m2. 352 s
Sys 0m0. 276 s
Root @ debian :~ /Coding/php/test # time php pai_test.php
Real 0m2. 689 s
User 0m1. 864 s
Sys 0m0. 804 s
Root @ debian :~ /Coding/php/test # time php pai_test.php
Real 0m2. 700 s
User 0m2. 460 s
Sys 0m0. 216 s
Root @ debian :~ /Coding/php/test # time php foreach_test.php
Real 0m2. 227 s
User 0m2. 016 s
Sys 0m0. 188 s
Root @ debian :~ /Coding/php/test # time php foreach_test.php
Real 0m2. 276 s
User 0m2. 056 s
Sys 0m0. 200 s
I don't know why.
600 | $ v <500) {continue;} yield $ arr [$ v] ;}// generate a test array $ arr = []; for ($ I = 0; $ I <10000; $ I ++) {$ k = mt_rand (1, 1000000); $ arr [$ k] = 'cdddss';} // obtain the array key, sort keys. Use the generator to retrieve data with a key value between and $ st = microtime (true); $ key = array_keys ($ arr); sort ($ key ); $ result = array (); foreach (getItem ($ key, $ arr) as $ v) {$ result [] = $ v ;} echo (memory_get_usage ()/1024/1024 ). "M \ n"; echo microtime (true)-$ st. "\ n"; echo "Number of original Arrays :". count ($ arr ). "\ n"; echo "Number of result arrays ". count ($ result ). "\ n ";
According to the question, we can see that the array length is fixed, 1000, so array_key obtains the key value sort sorting.
The generator then obtains the value of the demand interval.
Advantage: Low built-in occupation, stable and reliable performance
PHP5.5 is required
Ps: none of the above answers carefully read the subject's questions. He needs to return data based on the key. The number of keys is fixed at 10000, sosort(array_keys($arr))
You can.
If efficiency is to be discussed, we do not recommend creating an array with 10000 elements.
Use
array_walk()
You do not need to use foreach on the php layer, but the implementation of array_walk actually traverses the entire hashtable.
Unless the order is arranged, how can we ensure that data will not be missed without traversing?