0

I have two arrays that I'm trying to compare. One of the arrays I need to remove the values in it, if the values are present at least once in the first array. Here's what the arrays look like:

array1: {
    1: {
        0: "1"
    },
    1: {
        0: "1"
    },
    24: {
        0: "24"
    },
    24: {
        0: "24"
    },
    24: {
        0: "24"
    },
    24: {
        0: "24"
    },
    26: {
        0: "26"
    }
},

array2: {
    1: {
        0: "blue"
    },
    23: {
        0: "yellow"
    },
    24: {
        0: "red"
    },
    26: {
        0: "green"
    }
},

What I need to do is check array1 keys and if array2 has those same values in the key remove them from array 2. So for this example I should only end up having

array2: {
   23: {
       0: "yellow"
   }
}

I'm having to do this for several different instances of arrays that look similar.

I have tried:

$result = array_diff($array1, $array2);
print_r($result);

and that returns:

{
    25: {
         0: "25"
    }
}
{
    24: {
         0: "24"
    }
}
{
    24: {
         0: "24"
    }
}

While I realize that it's returning those values because there are multiples of them in the first array. I'm wondering how can I get it to ignore the doubles. Also I don't understand why 23 was not returned.

4
  • A simple array_diff() would work for an ordinary problem, but it looks like you are dealing with arrays containing arrays of 1 element, instead of simply having an array with multiple elements. I don't know how you managed to get yourself in this situation but I'm pretty sure you should look this up first instead of writing nasty extensive code just to deal with this complication. Commented Aug 11, 2016 at 22:09
  • As I said in the original post I've tried using array_diff. I also did go to php.net/manual/en/function.array-diff.php and read the documentation there, to see if I could figure something out. I've been trying to get this to work for 2 days now, that's why I'm posting it here. Also I don't have a lot of control over the arrays. Commented Aug 11, 2016 at 22:13
  • @kamosabe That's what I'm saying, array_diff() wont work because you are not comparing the elements, you are comparing arrays to arrays because you have the actual information nested in a second level. Comparing arrays to arrays directly doesn't work in PHP and therefore array_diff() is useless in this case, however the real question is, why the hell you have it that way in the first place? Commented Aug 11, 2016 at 22:15
  • @Havenard One of the arrays (array2) I really have no control over it, the other array I did create the other array from other elements, I'm now reading up on how to make it match the array that can't be changed. Thanks, for the comments. I'm open to any other suggestions you or others might have. Commented Aug 11, 2016 at 22:40

1 Answer 1

2

Should work:

<?php 
    foreach($array1 as $a1){
        unset($array2[$a1[0]]);
    }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

You don't need to check if element is set. So, you can remove line 3 if condition.
For myself I did need to check for an array_key_exists.

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.