How can I compare the rows in two 2d arrays?

11,556

Solution 1

One way is to write a function to do something similar to this..

function compareArray ($array1, $array2)
{
  foreach ($array1 as $key => $value)
  {
    if ($array2[$key] != $value)
    {
      return false;
    }
  }

  return true;
}

You could easily augment that function to return an array of differences in the two..

Edit - Here's a refined version that more closely resembles what you're looking for:

function compareArray ($array1, $array2)
{
  var $differences;

  foreach ($array1 as $key => $value)
  {
    if ($array2[$key] != $value)
    {
      $differences[$key][1] = $value;
      $differences[$key][2] = $array2[$key];
    }
  }

  if (sizeof($differences) > 0)
  {
    return $differences;
  }
  else
  { 
    return true;
  }
}

Solution 2

I think this does what you're looking for.

Using your sample data, doing a loop on the outer arrays, then using array_diff_assoc on the users each time through. (Note, this assumes that when there's a difference, array_diff_assoc returns the value from the second array passed in, which it seems to do).

<?php
$user1 = array("public" => 1, "private" => 1, "secret" => 1);
$user2 = array("public" => 1, "private" =>1, "secret" => 1);
$array1 = array ("user 1"=>$user1, "user 2"=>$user2);

$user1 = array("public" => 1, "private" => 0, "secret" => 1);
$user2 = array("public" => 1, "private" => 1, "secret" => 1);
$array2 = array("user 1"=>$user1, "user 2"=>$user2);

$results = array();  
foreach ( $array1 as $user => $value )
{
    $diff = array_diff_assoc( $array1[$user], $array2[$user] );
    if ($diff) {
        array_push($results,array($user=>$diff));   
        }
}


print_r($results);


?>

It returns:

Array
(
    [0] => Array
        (
            [user 1] => Array
                (
                    [private] => 1
                )
        )    
)
Share:
11,556
Nathan Long
Author by

Nathan Long

I code mostly in Elixir, sometimes Ruby. More about me at nathanmlong.com and Stackoverflow Careers.

Updated on June 05, 2022

Comments

  • Nathan Long
    Nathan Long almost 2 years

    It seems that every PHP function I read about for comparing arrays (array_diff(), array_intersect(), etc) compares for the existence of array elements.

    Given two multidimensional arrays with identical structure, how would you list the differences in values?

    Example

    Array 1

    [User1] => Array ([public] => 1
                    [private] => 1
                    [secret] => 1
                   ) 
    [User2] => Array ([public] => 1
                    [private] => 0
                    [secret] => 0
                   )
    

    Array 2

    [User1] => Array ([public] => 1
                    [private] => 0
                    [secret] => 1
                   ) 
    [User2] => Array ([public] => 1
                    [private] => 0
                    [secret] => 0
                   )
    

    Difference

    [User1] => Array ([public] => 1
                    [private] => 0 //this value is different
                    [secret] => 1
                   )
    

    So my result would be - "Of all the users, User1 has changed, and the difference is that private is 0 instead of 1."