1
Array#1     
Array(
        [0] => Array(
            [id] => 0
            [name] => a
        )
        [1] => Array(
            [id] => 1
            [name] => b
        )
        [2] => Array(
            [id] => 2
            [name] => c
        )
    )

Array #2
Array(
    [0] => Array(
        [id] => 0
        [name] => c
    )
    [1] => Array(
        [id] => 1
        [name] => a
    )
)

I would like to unset the first array with the second array referencing by the name because the key changes all the time.

I'm stuck with looping with removing it with name than key with this. Any help is much appreciated!

I would like to remove whatever array#2 has in array#1.

Final Array 
Array(
         [0] => Array(
             [id] => 1
             [name] => b
         )
     }
5
  • 2
    be more clear and post the code you have tried Commented Aug 12, 2013 at 13:44
  • You could show your desired result to clarify what you are trying to do. And like @RohitKumarChoudhary said: Show your code. Commented Aug 12, 2013 at 13:45
  • You want to unset from first array all values (which are also arrays) that have name in the second array? Commented Aug 12, 2013 at 13:48
  • @ep0 yes that is what i want to achieve. sorry guys for not being clear, I've edited it. Commented Aug 12, 2013 at 13:50
  • Have you tried the built in function options? php.net/manual/en/ref.array.php alternatively, rather than someone having to write a full solution for you, show your current code as it may be a quick fix Commented Aug 12, 2013 at 13:51

1 Answer 1

2
$arr = Array(
    0 => array(
        'id' => 0,
        'name' => 'a'),
    1 => array(
        'id' => 1,
        'name' => 'b'),
    2 => array(
        'id' => 2,
        'name' => 'c'));
$arr2 = Array(
    0 => array(
        'id' => 0,
        'name' => 'c'),
    1 => array(
        'id' => 1,
        'name' => 'a'));


$ex = array_map(function($a) {return $a['name'];}, $arr2);
foreach ($arr as $key => $value){
    if (in_array($value['name'], $ex)){
        unset($arr[$key]);
    }
}
print_r($arr);

Output:

Array
(
    [1] => Array
        (
            [id] => 1
            [name] => b
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

hey man thanks a lot, this is perfect, just got what i needed. thanks again!

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.