Compare multidimensional arrays in PHP

前端 未结 6 840
情话喂你
情话喂你 2020-11-27 07:35

How can I compare multidimensional arrays in php? Is there a simple way?

相关标签:
6条回答
  • 2020-11-27 07:54

    The simplest way I know:

    $a == $b;
    

    Note that you can also use the ===. The difference between them is:

    1. With Double equals ==, order is important:

      $a = array(0 => 'a', 1 => 'b');
      $b = array(1 => 'b', 0 => 'a');
      var_dump($a == $b);  // true
      var_dump($a === $b); // false
      
    2. With Triple equals ===, types matter:

      $a = array(0, 1);
      $b = array('0', '1');
      var_dump($a == $b);  // true
      var_dump($a === $b); // false
      

    Reference: Array operators

    0 讨论(0)
  • 2020-11-27 08:01
    $difference = array();
    foreach($array1 as $key => $value)
    {
        if(is_array($value))
        {
            if(!isset($array2[$key]))
            {
                $difference[$key] = $value;
            }
            elseif(!is_array($array2[$key]))
            {
                $difference[$key] = $value;
            }
            else
            {
                $new_diff = array_diff($value, $array2[$key]);
                if($new_diff != FALSE)
                {
                    $difference[$key] = $new_diff;
                }
            }
        }
        elseif(!isset($array2[$key]) || $array2[$key] != $value)
        {
            $difference[$key] = $value;
        }
    }
    
    0 讨论(0)
  • 2020-11-27 08:03
    function multi_diff($arr1,$arr2){
      $result = array();
      foreach ($arr1 as $k=>$v){
        if(!isset($arr2[$k])){
          $result[$k] = $v;
        } else {
          if(is_array($v) && is_array($arr2[$k])){
            $diff = multi_diff($v, $arr2[$k]);
            if(!empty($diff))
              $result[$k] = $diff;
          }
        }
      }
      return $result;
    }
    
    //example:
    
    var_dump(multi_diff(
    
    array(
      "A"=>array(
        "A1"=>array('A1-0','A1-1','A1-2','A1-3'),
        "A2"=>array('A2-0','A2-1','A2-2','A2-3'),
        "A3"=>array('A3-0','A3-1','A3-2','A3-3')
      ),
      "B"=>array(
        "B1"=>array('B1-0','B1-1','B1-2','B1-3'),
        "B2"=>array('B2-0','B2-1','B2-2','B2-3'),
        "B3"=>array('B3-0','B3-1','B3-2','B3-3')
      ),
      "C"=>array(
        "C1"=>array('C1-0','C1-1','C1-2','C1-3'),
        "C2"=>array('C2-0','C2-1','C2-2','C2-3'),
        "C3"=>array('C3-0','C3-1','C3-2','C3-3')
      ),
      "D"=>array(
        "D1"=>array('D1-0','D1-1','D1-2','D1-3'),
        "D2"=>array('D2-0','D2-1','D2-2','D2-3'),
        "D3"=>array('D3-0','D3-1','D3-2','D3-3')
      )
    ),
    
    array(
      "A"=>array(
        "A1"=>array('A1-0','A1-1','A1-2','A1-3'),
        "A2"=>array('A2-0','A2-1','A2-2','A2-3'),
        "A3"=>array('A3-0','A3-1','A3-2')
      ),
      "B"=>array(
        "B1"=>array('B1-0','B1-2','B1-3'),
        "B2"=>array('B2-0','B2-1','B2-2','B2-3'),
        "B3"=>array('B3-0','B3-1','B3-3')
      ),
      "C"=>array(
        "C1"=>array('C1-0','C1-1','C1-2','C1-3'),
        "C3"=>array('C3-0','C3-1')
      ),
      "D"=>array(
        "D1"=>array('D1-0','D1-1','D1-2','D1-3'),
        "D2"=>array('D2-0','D2-1','D2-2','D2-3'),
        "D3"=>array('D3-0','D3-1','D3-2','D3-3')
      )
    )
    
    ));
    
    0 讨论(0)
  • 2020-11-27 08:12

    This function will do it all for you.

    You can use it to truly compare any 2 arrays of same or totally different structures.

    It will return:

    Values in array1 not in array2 (more)

    Values in array2 not in array1 (less)

    Values in array1 and in array2 but different (diff)

    //results for array1 (when it is in more, it is in array1 and not in array2. same for less)
    function compare_multi_Arrays($array1, $array2){
        $result = array("more"=>array(),"less"=>array(),"diff"=>array());
        foreach($array1 as $k => $v) {
          if(is_array($v) && isset($array2[$k]) && is_array($array2[$k])){
            $sub_result = compare_multi_Arrays($v, $array2[$k]);
            //merge results
            foreach(array_keys($sub_result) as $key){
              if(!empty($sub_result[$key])){
                $result[$key] = array_merge_recursive($result[$key],array($k => $sub_result[$key]));
              }
            }
          }else{
            if(isset($array2[$k])){
              if($v !== $array2[$k]){
                $result["diff"][$k] = array("from"=>$v,"to"=>$array2[$k]);
              }
            }else{
              $result["more"][$k] = $v;
            }
          }
        }
        foreach($array2 as $k => $v) {
            if(!isset($array1[$k])){
                $result["less"][$k] = $v;
            }
        }
        return $result;
    }
    
    0 讨论(0)
  • 2020-11-27 08:13

    To compare array's structure, You should use identity operator.

    if($arrayA === $arrayB) {
        ...
    }
    
    0 讨论(0)
  • 2020-11-27 08:20

    Another way to do it is to serialize() both of the arrays and compare the strings.

    http://php.net/manual/en/function.serialize.php

    0 讨论(0)
提交回复
热议问题