Home > Backend Development > PHP Tutorial > Differences between array_diff and other ways to implement PHP array traversal

Differences between array_diff and other ways to implement PHP array traversal

巴扎黑
Release: 2023-03-07 21:34:02
Original
1581 people have browsed it

Give you two arrays with 5000 elements each, and calculate their difference. To put it bluntly, it means using PHP and the algorithm you think is the best to implement the array_diff algorithm.

When I received this question for the first time, I found it to be very simple, so I wrote one based on my past experience:

function array_diff($array_1, $array_2) { 
    $diff = array(); 
 
    foreach ($array_1 as $k => $v1) { 
        $flag = false; 
        foreach ($array_2 as $v2) { 
            if ($flag = ($v1 == $v2)) { 
                break; 
            } 
        } 
 
        if (!$flag) { 
            $diff[$k] = $v1; 
            
        } 
    } 
 
    return $diff; 
}
Copy after login

Although the implementation is possible, I found that the efficiency of this function is appalling. So I reconsidered and optimized the algorithm. The second function looked like this:

function array_diff($array_1, $array_2) { 
    foreach ($array_1 as $key => $item) { 
        if (in_array($item, $array_2, true)) { 
            unset($array_1[$key]); 
        } 
    } 
 
    return $array_1; 
}
Copy after login

Well, this time it is almost as fast as the original array_diff function. But is there a more optimized way? From an article on ChinaUnix, I discovered that PHP can be written like this:

function array_diff($array_1, $array_2) { 
    $array_2 = array_flip($array_2); 
    foreach ($array_1 as $key => $item) { 
        if (isset($array_2[$item])) { 
            unset($array_1[$key]); 
        } 
     } 
 
    return $array_1; 
}
Copy after login

The efficiency of this function is very amazing, even faster than the original array_diff function. Investigating the reason, I found an explanation: because the keys are organized by HASH, the search is very fast; while the Value is only stored in the Key organization and has no index itself, so every search is traversed.

Summary

Although this is a little trick of the PHP language, when traversing and comparing the values ​​​​of the array, if you need to compare the value, reversing it with the key is indeed better than the usual value pair. The comparison efficiency is much higher.

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 above is the detailed content of Differences between array_diff and other ways to implement PHP array traversal. For more information, please follow other related articles on the PHP Chinese website!

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