0

so for example I have an associative array like

array( "a" => 23, "b" => 48, "c" => 10, "d" => 19 )

Let's call it ArrayA. And another array (ArrayB) that is

array( "a", "c" )

I want to get ArrayA's keys that do not occur in ArrayB, which would be "b" and "d".

I haven't found anything useful when googling but I assume that there is a php function for that or how would you solve this as fancy as possible?

4
  • 1
    array_diff_key($arrayA, array_flip($arrayB)) Commented Nov 25, 2016 at 17:10
  • Possible duplicate of Compare all values in php array with the other values Commented Nov 25, 2016 at 17:12
  • array_diff(array_keys($a), $b) Commented Nov 25, 2016 at 17:14
  • thank you Mark and Ruslan, this is what I expected :) both work, I'll go with the shorter one Commented Nov 25, 2016 at 17:18

1 Answer 1

0

You can doit with array_diff() and array_keys()
Manual array_diff: http://php.net/manual/en/function.array-diff.php
Manual array_keys: http://php.net/manual/en/function.array-keys.php

With array_diff() the function returns an array containing all the entries from array1 that are not present in any of the other arrays.
And with array_keys() will return the keys, numeric and string, from the array. You can add specific search options with this function to.

<?php
$ArrayA = array("a" => 23, "b" => 48, "c" => 10, "d" => 19);
$ArrayB = array("a", "c");
$result = array_diff(array_keys($ArrayA), $ArrayB);

print_r($result);
?>

Regards

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.