0

I have some kind of specific problem. I have multidimensional array which looks like:

array(2) {
  [0]=>
  array(6) {
    [0]=>
    array(9) {
      ["id"]=>
      int(9997)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(11) "Web Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "1" 
    }
    [1]=>
    array(9) {
      ["id"]=>
      int(9998)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
     }
    [2]=>
    array(9) {
      ["id"]=>
      int(9999)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "3"
    }
    [3]=>
    array(9) {
      ["id"]=>
      int(10000)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
    [4]=>
    array(9) {
      ["id"]=>
      int(10001)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
    [5]=>
    array(9) {
      ["id"]=>
      int(10002)
      ["project_id"]=>
      int(327)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(11) "Web Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
  }
  [1]=>
  array(5) {
    [0]=>
    array(9) {
      ["id"]=>
      int(8828)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(11) "Web Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "1"
    }
    [1]=>
    array(9) {
      ["id"]=>
      int(8829)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "3"
    }
    [2]=>
    array(9) {
      ["id"]=>
      int(8830)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
    [3]=>
    array(9) {
      ["id"]=>
      int(8831)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(7) "Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
    [4]=>
    array(9) {
      ["id"]=>
      int(8832)
      ["project_id"]=>
      int(298)
      ["projectType_id"]=>
      int(1)
      ["parameter"]=>
      string(11) "Web Server?"
      ["description"]=>
      NULL
      ["severity"]=>
      string(1) "2"
    }
  }
}

What I would like to do is to knew about differences between those tables. I mean how many rows are present in table 1 and are absent in table 2. How many rows are absent in table 1 and present in table 2 and how many rows are equals in both tables.

I tried with creating loops over array checking each row and comparing it. My code is posted bellow. It got me nowhere..

foreach ($bigArray[0] as $new){
    foreach($bigArray[1] as $old){
        if ($new== $old)
            echo "it is the same";
        else
            echo "it is different";
    }
}

All I got is it is different all time...

Is there any function to count this kind of stuff? (first column -ID is unique for each row so it should not have been taken into consideration.

3
  • A good starting point would be by start looking at array_diff: us1.php.net/manual/en/function.array-diff.php Commented Jan 28, 2014 at 8:08
  • 1
    How you want two rows to be compared? by 'id' prop or by full content? Commented Jan 28, 2014 at 8:16
  • everything but the id(which is different for every row) Commented Jan 28, 2014 at 8:17

1 Answer 1

2

You have to use array_udiff() in conjunction with array_diff_assoc(). Somehow like this:

function compare($a, $b) {
    if (isset($a['id'])) unset($a['id']);
    if (isset($b['id'])) unset($b['id']);
    return count(array_diff_assoc($a, $b))? 0 : 1;
}

$result = array_udiff($bigArray[0], $bigArray[1], 'compare');
if (!count($result)) echo 'it is the same';
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.