0

I've got these two arrays:

Array ONE
(
    [39] => Dude, Harry [10%]
    [36] => Occonell, Tim [90%]
)

Array TWO
(
    [1] => Person, Admin
    [2] => Snow, John
    [3] => Jane, Marry
    [14] => Kelchenko, Igory
    [18] => Bery, Grass
    [36] => Occonell, Tim
)

I'm trying to delete value from second array, if it's key is equal to first one. So my array should look like:

Array TWO
(
    [1] => Person, Admin
    [2] => Snow, John
    [3] => Jane, Marry
    [14] => Kelchenko, Igory
    [18] => Bery, Grass
)

I have attempted to find an answer else where, but I could not find one.

2 Answers 2

3

array_diff() is in the right direction, but you want to do stuff with the keys of the arrays, so use: array_diff_key(), e.g.

print_r(array_diff_key($array2, $array1));

As an example:

$array2 = [
    1 => "a",
    2 => "b",
    3 => "c",
    4 => "d",
    5 => "e",
]; 

$array1 = [
    2 => "Don't want this key",
    4 => "Also don't want this one",
];

output will be:

Array ( 
    1 => a
    3 => c
    5 => e
)
Sign up to request clarification or add additional context in comments.

Comments

3

you can do following

foreach($arr_one as $key => $val){
    if(isset($array_2[$key]))    unset($array_2[$key]);
}

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.