1

Probably a duplicate...

Array1 = ['a'=>1, 'b'=>2, 'c'=>3];
Array1 = ['a'=>1, 'b'=>2, 'c'=>'anaconda'];
$keys = ISTHEREAFUCNTION(array1, array2);

echo ($keys);

// c

Is there a function that compares arrays by value and return keys of array1 values different from values of array2?

I can do it just iterating over both arrays, but maybe there is a more elegant solution?

Thank you!

1

2 Answers 2

1

Using array_diff() and array_keys()

$ cat test.php
<?php

$arr1 = ['a'=>1, 'b'=>2, 'c'=>3];
$arr2 = ['a'=>1, 'b'=>2, 'c'=>'anaconda'];
print_r(array_keys(array_diff($arr1,$arr2)));

?>

$ php test.php
Array
(
    [0] => c
)
Sign up to request clarification or add additional context in comments.

1 Comment

thank you! feel stupid now) I thought that array_diff returns only diff.values, and re-writes the keys:)
0

Use array_keys and array_diff_assoc

array_keys(array_diff_assoc($array1, $array2));

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.