The difference of PHP array traversal (implementation of array_diff)

time:13-06-16 forum:PHP tools and code
  Reprinted from http://blog.csdn.net/zuiaituantuan/article/details/6032840

the following topic:

give you two arrays of 5000 elements respectively, calculate their difference set

- that is, the algorithm that uses PHP and the best algorithm that you think is the best algorithm.

first received the topic, and I found it very simple, so I wrote a "

[code]function array_diff", "{

$diff = array." Flag = false;

foreach ($array_2 as $v2) {

if ($flag = = > > > > = = > = > > > > > > > > > > > > > > > > > > > > > }

}



return $diff;

}[/code]

although it is possible, it is disastrous to find the efficiency of this function. Then I reconsidered and optimized the algorithm, and the second functions look like this:

[code]function array_diff ($array_1, $array_2) {

foreach ($array_1 as $key). ET ($array_1.#91; $key.#93;);

}

}



return / > well, this time it is almost as fast as the original function. But is there a better way to do it? By an article on ChinaUnix (embarrassed, cheating), I found that PHP can write this:

[code]function array_diff ($array_1, $array_2) {

$array_2 = array_flip. Rray_2.#91; $item.#93;)) {

unset ($array_1.#91; $key.#93;);

}

this function is very efficient, even faster than the original function. For the reason, I found the explanation:

because the key is the HASH organization, lookup quickly;

and Value is only stored by the Key organization, itself has no index, each lookup is traversal.

summing up

this is a trick of the PHP language, but it is much more efficient to compare the values of the comparison array with the value of the key in the values of the traversal and contrast array.

for example, the function two above needs to call the in_array function to circulate to judge whether it is within the function; and the function three can only judge whether the array has the key. With array key and different index of organization index, efficiency is higher than imagination, which is very understandable.

prev:PHP gets the intersection and difference... next:
post comment
Notice



approved   The inheritance of the old Ji'nan ha...   Do not belittle those street cars, t...   South Korea's chief mother is popula...   The 3 year old was elected the young...   The Qilu Public Welfare Alliance has...
rand   Using CURL instead of file_get_conte...   Use.Htaccess to prevent images from ...   Encryption and decryption of PHP fil...   PHP hidden data, MD5 encryption, int...   Processing method of Chinese charact...
ads

other

Because all the contents of this site are reproduced from other media, the intention is to provide free services to the public, but it does not mean that this site agrees with its views, and can not be responsible for the authenticity of its verification, such as the copyright unit or individual of the manuscript does not want to publish on this site, please Contact me, I will immediately remove it, thank you. Contact: atseashawk@163.com