Home > Backend Development > PHP Tutorial > Differences about php array traversal (array_diff application example)

Differences about php array traversal (array_diff application example)

WBOY
Release: 2016-07-25 08:58:38
Original
872 people have browsed it
  1. function array_diff($array_1, $array_2) {

  2. $diff = array();

  3. foreach ($array_1 as $k => $v1) {

  4. $flag = false;
  5. foreach ($array_2 as $v2) {
  6. if ($flag = ($v1 == $v2)) {
  7. break;
  8. }
  9. }
  10. if (!$flag) {

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

  14. return $diff;

  15. }
  16. ?>
  17. < ;/p>
Copy code

The implementation of the above code is a bit far-fetched. So I reconsidered it and optimized the algorithm. The second function looked 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 it is almost as fast as the original array_diff function. But is there a more optimized way? I found that PHP can be written like this:

  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

The efficiency of this function is amazing, even faster than the original array_diff function. Investigating the reason, I found the explanation:

Because the keys are HASH organized, the search is fast; Value is only organized and stored by Key. It has no index itself, and each search is traversed. Summarize Although this is a little trick of the PHP language, when traversing and comparing array values, if you need to compare the value, reversing it with the key is indeed much more efficient than the usual value-to-value comparison.

For example, function two above needs to call the in_array function and needs to loop to determine whether it is within the function; while function three only determines whether the key exists in the array. Coupled with the different organizational indexing methods of array keys and values, it is very understandable that the efficiency is higher than imagined.

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 => $v1) {

  8. $flag = false;
  9. foreach ($array_2 as $v2) {
  10. if ($flag = ($v1 == $v2)) {
  11. break;
  12. }
  13. }

  14. if (!$flag) {

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

  18. return $diff;

  19. }< /p>
  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 < 5000; $i++) {

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

  41. for($i = 0, $ary_2 = array(); $i < 5000; $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 runs" . (microtime_float() - $time_start) . " seconds n";

  51. $time_start = microtime_float ();

  52. array_diff3($ary_1, $ary_2);
  53. echo "Function array_diff3 runs" . (microtime_float() - $time_start) . "Seconds";

  54. $time_start = microtime_float( );

  55. array_diff4($ary_1, $ary_2);
  56. echo "Function array_diff4 run" . (microtime_float() - $time_start) . "Seconds";
  57. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template