Differences in PHP array traversal (Array_diff application example)

Source: Internet
Author: User
Tags php language
    1. function Array_diff ($array _1, $array _2) {

    2. $diff = Array ();

    3. foreach ($array _1 as $k = + $v 1) {

    4. $flag = false;
    5. foreach ($array _2 as $v 2) {
    6. if ($flag = ($v 1 = = = $v 2)) {
    7. Break
    8. }
    9. }

    10. if (! $flag) {

    11. $diff [$k] = $v 1;
    12. }
    13. }

    14. return $diff;

    15. }
    16. ?>
Copy Code

The implementation of the above code is a bit farfetched. So I reconsidered and optimized the algorithm, and the second function looks like this:

    1. function Array_diff ($array _1, $array _2) {

    2. foreach ($array _1 as $key = + $item) {
    3. if (In_array ($item, $array _2, True)) {
    4. unset ($array _1[$key]);
    5. }
    6. }

    7. return $array _1;

    8. }
    9. ?>
Copy Code

This time is almost comparable to the speed of the original Array_diff function. But is there any way to optimize it? Find PHP can write this way:

    1. function Array_diff ($array _1, $array _2) {

    2. $array _2 = Array_flip ($array _2);
    3. foreach ($array _1 as $key = + $item) {
    4. if (Isset ($array _2[$item])) {
    5. unset ($array _1[$key]);
    6. }
    7. }

    8. return $array _1;

    9. }
    10. ?>
Copy Code

This function is surprisingly efficient, even faster than the original Array_diff function. The reason, I found the explanation:

Because the keys are organized in a HASH, the lookup is quick, and Value is stored by the key organization, which itself is not indexed, and each lookup is traversed. Summarizing this is a small trick of the PHP language, but in traversing and contrasting the values of the array, it is much more efficient to compare the value to the key than the usual value if it is necessary to reverse the value.

For example, the function two above needs to call the In_array function needs to loop to determine whether the function inside, and the function of the three only to determine whether the array exists the key is OK. With the array key and the value of the organization index of the different way, more efficient than the imagination that is very understandable.

The complete code is as follows:

  1. function Microtime_float () {

  2. List ($usec, $sec) = Explode ("", Microtime ());
  3. return (float) $usec + (float) $sec);
  4. }

  5. function array_diff2 ($array _1, $array _2) {

  6. $diff = Array ();

  7. foreach ($array _1 as $k = + $v 1) {

  8. $flag = false;
  9. foreach ($array _2 as $v 2) {
  10. if ($flag = ($v 1 = = = $v 2)) {
  11. Break
  12. }
  13. }

  14. if (! $flag) {

  15. $diff [$k] = $v 1;
  16. }
  17. }

  18. return $diff;

  19. }

  20. function array_diff3 ($array _1, $array _2) {
  21. foreach ($array _1 as $key = + $item) {
  22. if (In_array ($item, $array _2, True)) {
  23. unset ($array _1[$key]);
  24. }
  25. }

  26. return $array _1;

  27. }

  28. function Array_diff4 ($array _1, $array _2) {
  29. $array _2 = Array_flip ($array _2);
  30. foreach ($array _1 as $key = + $item) {
  31. if (Isset ($array _2[$item])) {
  32. unset ($array _1[$key]);
  33. }
  34. }

  35. return $array _1;

  36. }

  37. //////////////////////////////

  38. for ($i = 0, $ary _1 = Array (), $i < $i + +) {

  39. $ary _1[] = rand (100, 999);
  40. }

  41. for ($i = 0, $ary _2 = Array (), $i < $i + +) {

  42. $ary _2[] = rand (100, 999);
  43. }

  44. Header ("Content-type:text/plain;charset=utf-8");

  45. $time _start = Microtime_float ();

  46. Array_diff ($ary _1, $ary _2);
  47. echo "function Array_diff run". (Microtime_float ()-$time _start). "Seconds \ n";

  48. $time _start = Microtime_float ();

  49. ARRAY_DIFF2 ($ary _1, $ary _2);
  50. echo "function array_diff2 run". (Microtime_float ()-$time _start). "Seconds \ n";

  51. $time _start = Microtime_float ();

  52. ARRAY_DIFF3 ($ary _1, $ary _2);
  53. echo "function array_diff3 run". (Microtime_float ()-$time _start). "Seconds \ n";

  54. $time _start = Microtime_float ();

  55. Array_diff4 ($ary _1, $ary _2);
  56. echo "function Array_diff4 run". (Microtime_float ()-$time _start). "Seconds \ n";
  57. ?>

Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.