-2

Array1:

array('13','15','19');

Array2:

array('13','19');

Expected output:

2

Program:

$array1 = array('13','15','19');
$array2 = array('13','19');
print_r(array_diff($array1, $array2));

array_diff giving me value which value is different but instead it I want key. How can I get key?

2
  • 3
    Why 2? The key of 15 is 1 Commented May 21, 2018 at 12:39
  • Possible duplicate of PHP: Get key from array? I reckon this covers all the bases. Commented May 21, 2018 at 12:47

1 Answer 1

4

I think you're looking for :

$array1 = array('13','15',17,'19',21);
$array2 = array('13','19');
print_r(array_keys(array_diff($array1, $array2)));

which will print you 1,2 and 4 - the keys. (remember "1" means second, because we're starting with 0 key)

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

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.