0

I var_dumped two arrays, the top on is the array coming in, $new_array, while the other one is a preexisting array $current_array:

// New Array
array(3) {
  ["Contributor"]=>
  array(2) {
    [0]=>
    string(13) "edit_carousel"
    [1]=>
    string(13) "read_carousel"
  }
  ["Editor"]=>
  array(1) {
    [0]=>
    string(16) "delete_mini_feed"
  }
  ["Author"]=>
  array(3) {
    [0]=>
    string(11) "edit_blocks"
    [1]=>
    string(12) "edit_blockss"
    [2]=>
    string(12) "edit_blockss"
  }
}

// Preexisting
array(3) {
  ["Contributor"]=>
  array(2) {
    [0]=>
    string(13) "edit_carousel"
    [1]=>
    string(13) "read_carousel"
  }
  ["Editor"]=>
  array(4) {
    [0]=>
    string(16) "delete_mini_feed"
    [1]=>
    string(15) "edit_mini_feeds"
    [2]=>
    string(23) "edit_private_mini_feeds"
    [3]=>
    string(15) "edit_mini_feeds"
  }
  ["Author"]=>
  array(3) {
    [0]=>
    string(11) "edit_blocks"
    [1]=>
    string(12) "edit_blockss"
    [2]=>
    string(12) "edit_blockss"
  }
}

I am trying to do something like: var_dump(array_intersect_assoc($current_array, $new_array)); to see whats different in the current array as opposed to the new array and generate an array of "differences" keeping the structure intact.

The issue is:

  1. Is the order of arrays to be compared right? compare old to new and get an array of whats different in old. or should it be compare new to old?
  2. Doing this, results in: Array to string conversion notice, but also prints out an array which is below.

I cant tell if these are: "these are whats not in old, but in new" or "the are whats not in new but in old" .... (It should say: these are not whats in old but in new).

array(3) {
  ["Contributor"]=>
  array(2) {
    [0]=>
    string(13) "edit_carousel"
    [1]=>
    string(13) "read_carousel"
  }
  ["Editor"]=>
  array(4) {
    [0]=>
    string(16) "delete_mini_feed"
    [1]=>
    string(15) "edit_mini_feeds"
    [2]=>
    string(23) "edit_private_mini_feeds"
    [3]=>
    string(15) "edit_mini_feeds"
  }
  ["Author"]=>
  array(3) {
    [0]=>
    string(11) "edit_blocks"
    [1]=>
    string(12) "edit_blockss"
    [2]=>
    string(12) "edit_blockss"
  }
}
LogicLooking
  • 916
  • 1
  • 16
  • 32

2 Answers2

0

The php function array_intersect_assoc() should return everything in the first array (aka $current_array) that exists in the second array (aka $new_array).

The issue that you are running into is that array_intersect_assoc() doesn't preform the key comparison recursively. It's only comparing the first level of keys (Contributor, Editor and Author).

Here's more information about the recursive issue. PHP Question: how to array_intersect_assoc() recursively

Community
  • 1
  • 1
Nick Mallare
  • 180
  • 7
  • So my ordering is correct? I wanted - everything thats in new_array thats **NOT** in current array to be returned – LogicLooking Nov 13 '13 at 22:21
  • Well, first of all it won't return what you want it to because you're using multi-dimensional arrays. But, if they were both one dimension, using array_intersect_assoc() will return the common items. If you want to return the difference (aka what's in current and not in new) you would use array_diff_assoc($current_array, $new_array). – Nick Mallare Nov 13 '13 at 22:31
  • And then Im assuuming because of the multiple dimensions that it wont just work out of the box? - array_diff_assoc that is .. – LogicLooking Nov 13 '13 at 22:38
  • One way to do it would be to flatten your dimensions. Use array_diff($current_array['Contributor'], $new_array['Contributor']); for instance. But, what is your end goal. Do you just want to know which settings the system user took away from each user type? – Nick Mallare Nov 13 '13 at 22:42
0

Your problem is that you are trying to perform array_intersect on a multidimensional array, but the function does string comparison of elements, resulting in array to string conversion error.

As you have the same keys in both arrays, the simplest solution is just to foreach through and to compare subsequent arrays(and you rather need array_diff if you want difference between the elements)

foreach($array_1 as $index => $sub_array) {
  $sub_array_2 = $array_2[$index];
  $diff = array_diff($sub_array, $sub_array_2);
  // do something with diff
}

UPDATE

If You want to get everything that's not in array_1 but in array_2:

$result = [];

# lets find if there's any new elements
$keys_1 = array_keys($array_1);
$keys_2 = array_keys($array_2);

$diff = array_diff($keys_1, $keys_2);

if(!empty($diff)) {
  foreach($diff as $key) { 
    if(isset($array_2[$key])) {
      # it's in array_2
      $result[$key] = $array_2[$key];
    }
  }
}

# now get difference between shared elements
$intersection = array_intersect($keys_1, $keys_2);

foreach($intersection as $key) {
  $element_1 = $array_1[$key];
  $element_2 = $array_2[$key];

  $diff = array_diff($element_1, $element_2);
  if(sizeof($diff)) {
    if(!isset($result[$key]) ||!is_array($result[$key]) ) {
      $result[$key] = array($diff);
    } else {
      $result[$key][] = $diff;
    }
  }
}
baldrs
  • 2,132
  • 25
  • 34
  • What if I didn't have the same keys? would your solution still work? for example if `Contributor` in `$new_array` was changed to `Contributor_2` ... @Baldrs – LogicLooking Nov 13 '13 at 22:19
  • You can still do diff between just keys, as follows `$diff = array_diff(array_keys($array1), array_keys($array2));` If there's any difference in keys code in my answer does not have any sense. – baldrs Nov 13 '13 at 22:23