PHP compare array

后端 未结 17 1768
萌比男神i
萌比男神i 2020-11-28 12:19

Is there anyway to compare arrays in php using an inbuilt function, short of doing some sort of loop?

$a1 = array(1,2,3);
$a2 = array(1,2,3);

if (array_are_         


        
相关标签:
17条回答
  • 2020-11-28 13:09

    Compare values of two arrays:

    $a = array(1,2,3);
    $b = array(1,3,2);
    
    if (array_diff($a, $b) || array_diff($b, $a)) {
        echo 'Not equal';
    }else{
        echo 'Equal';
    }
    
    0 讨论(0)
  • 2020-11-28 13:11

    Also you can try this way:

    if(serialize($a1) == serialize($a2))
    
    0 讨论(0)
  • 2020-11-28 13:11

    Just check $a1 == $a2 -- what's wrong with that?

    0 讨论(0)
  • 2020-11-28 13:13

    If you want to search some array inside a big array of arrays (>10k) then much more quickly is comparing serialized arrays (saved in cache). Example from work with URL:

    /**
    @return array
    */
    function createCache()
    {
        $cache = [];
        foreach ($this->listOfUrl() as $url => $args)
        {
            ksort($args);
            $cache['url'][$url] = $args;
            $cache['arr'][crc32(serialize($args))] = $url;
        }
        return $cache;
    }
    
    /**
    @param array $args
    @return string
    */
    function searchUrl($args)
    {
        ksort($params);
        $crc = crc32(serialize($params));        
        return isset($this->cache['arr'][$crc]) ? $this->cache['arr'][$crc] : NULL;                
    } 
    
    /**
    @param string $url
    @return array
    */
    function searchArgs($url)
    {
        return isset($this->cache['url'][$url]) ? $this->cache['url'][$url] : NULL;
    }
    
    0 讨论(0)
  • 2020-11-28 13:15

    @Cleanshooter i'm sorry but this does not do, what it is expected todo: (so does array_diff mentioned in this context)

    $a1 = array('one','two');
    $a2 = array('one','two','three');
    
    var_dump(count(array_diff($a1, $a2)) === 0); // returns TRUE
    

    (annotation: you cannot use empty on functions before PHP 5.5) in this case the result is true allthough the arrays are different.

    array_diff [...] Returns an array containing all the entries from array1 that are not present in any of the other arrays.

    which does not mean that array_diff is something like: array_get_all_differences. Explained in mathematical sets it computes:

    {'one','two'} \ {'one','two','three'} = {}
    

    which means something like all elements from first set without all the elements from second set, which are in the first set. So that

    var_dump(array_diff($a2, $a1));
    

    computes to

    array(1) { [2]=> string(5) "three" } 
    

    The conclusion is, that you have to do an array_diff in "both ways" to get all "differences" from the two arrays.

    hope this helps :)

    0 讨论(0)
  • 2020-11-28 13:15

    verify that the intersections count is the same as both source arrays

    $intersections = array_intersect($a1, $a2);
    $equality = (count($a1) == count($a2)) && (count($a2) == count($intersections)) ? true : false;
    
    0 讨论(0)
提交回复
热议问题