0

I am pretty new with arrays and multi-dimensional arrays, and am wondering how to go about comparing the key=>value pairs of two arrays (or a single array with two different keys that contain arrays?) and unset the key=>value pairings from each array that match keys and value.

Example Array:

Array
(
    [from] => Array
        (
            [active] => 1
            [airport_ids] => 
            [group_name] => test adgrp
            [zone_id] => 12
            [creation_time] => 1234567890
        )

    [to] => Array
        (
            [active] => 1
            [airport_ids] => 
            [group_name] => test adgroup
            [zone_id] => 2
            [group_email] => test@group.com
        )

)

So, from is the base key array and to is the comparison key array. I want to iterate through both to and from key arrays, finding matching keys, and comparing the values of the two matches.

If they key=>value pair matches, unset the key=>value from both to and from key arrays.

if there is a key found in to that is not in from, leave it in the to array. However, if there is a key found in from that is not found in to, unset it from the from key array.

Turning the above array into something like this:

Array
(
    [from] => Array
        (
            [group_name] => test adgrp
            [zone_id] => 12
        )

    [to] => Array
        (
            [group_name] => test adgroup
            [zone_id] => 2
            [group_email] => test@group.com
        )

)
Meta
  • 1,830
  • 6
  • 24
  • 28
  • 1
    duplicate: http://stackoverflow.com/questions/8691329/php-how-to-compare-two-arrays-and-remove-duplicate-values – mickmackusa Feb 22 '17 at 00:00
  • @mickmackusa its a multidimensional array not a single array – lazyCoder Feb 22 '17 at 05:10
  • @Meta in from and to [group_name] => test adgroup is also common so why you want this in [to] ?? – lazyCoder Feb 22 '17 at 05:18
  • @BunkerBoy it the answer at my link is processing two arrays. If I am reading your array correctly, you are comparing `$array["from"]` and `$array["to"]` , right? – mickmackusa Feb 22 '17 at 07:49
  • 1
    Possible duplicate of [PHP - How to compare two arrays and remove duplicate values](http://stackoverflow.com/questions/8691329/php-how-to-compare-two-arrays-and-remove-duplicate-values) – mickmackusa Feb 22 '17 at 09:00
  • @mickmackusa array_diff is not what I am looking for, as I want to be able to define special cases where the key=>value should be unset() from the existing array, versus remaining as a difference ( A 0 value in the `from` may be the same as false or empty() or null on the `to` key=>value pair). – Meta Feb 24 '17 at 02:52
  • In that case, a better-suited duplicate is http://stackoverflow.com/a/16993746/2943403 which shows that BunkerBoy's solution takes that answer's function and adds a second condition statement and another element to unset(). – mickmackusa Feb 24 '17 at 03:17

1 Answers1

1

@Meta try this below concept:

<?php
    $arr1 = 
            array
            (
                "from" => array
                    (
                        "active" => 1,
                        "airport_ids" => null,
                        "group_name" => "test adgrp",
                        "zone_id" => 12,
                        "creation_time" => 1234567890
                    ),

                "to" => array
                    (
                        "active" => 1,
                        "airport_ids" => null,
                        "group_name" => "test adgroup",
                        "zone_id" => 2,
                        "group_email" => "test@group.com"
                    )

            );

    echo "<pre>";
    print_r($arr1); //before

    foreach($arr1["from"] as $key => $value) {
      foreach($arr1["to"] as $key1 => $value1) {
        if($key == $key1 && $value == $value1){
            unset($arr1["from"][$key], $arr1["to"][$key1]);
            break;
        }

      }
    }
    echo "<pre>";
    print_r($arr1); //after
lazyCoder
  • 2,544
  • 3
  • 22
  • 41
  • The reason that this is preferred above the array_diff() array, is there are cases where the values are different but not really different. For Example: if `from`.active == 0 and `to`.active is empty or null, technically that is the same as 0 in the system. However, the array_diff picks it up as a change between the two arrays. with the method listed above, I can do additional if statement to weed out those special cases. – Meta Feb 24 '17 at 02:49
  • @BunkerBoy when the condition statement is satisfied and unset() is used, add a `break;` as there will be no reason to further iterate the inner loop. As demonstrated @ http://stackoverflow.com/a/16993746/2943403 – mickmackusa Feb 24 '17 at 03:20