1

I have an array

$check = ['a', 'b', 'c'];

which I want to check against another array so that the values of $check should match the keys in $actual

$actual = ['a' => 'one', 'b' => 'two', 'c' => 'three'];

I can't use array_diff() === [] since array diff works on comparing the values and in this case I want to compare the values of one array against the keys in another.

6
  • 1
    use array_keys first. Then - array_diff Commented May 26, 2016 at 8:35
  • try array_diff_key... Commented May 26, 2016 at 8:42
  • Actually what you want to do?? Commented May 26, 2016 at 8:42
  • @FrayneKonok and if read the question? Commented May 26, 2016 at 8:42
  • Check this: https://3v4l.org/t9Nun Commented May 26, 2016 at 8:44

1 Answer 1

4

You can use array_keys();

<?php
$check = ['a', 'b', 'c'];
$actual = ['a' => 'one', 'b' => 'two', 'c' => 'three'];

$result = array_diff(array_keys($actual), $check);
print_r($result);

In this case array_diff returns a empty array because all keys are found

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.