1

I have two arrays one is self defined.

$fields = array( "first_name" => "test", "last_name" => "Test", "phone" =>"111-111-1111, "id" => 1234");

the other is grabbed from the first row of a csv file. which will return an array like so

$headers = array ("fname" => "test", "last_name" => "Test", "phone"=> "123-123-1234");

I then want to return an error if there is a key in $headers array doesn't match any of the keys in $fields array. Its okay if the keys in the $fields array aren't all present in the $headers array.

For example the two arrays above should return an error because the key fname does not exist in the $fields array but not because the id is missing in the $headers array.

I tried experimenting with if statements with multiple loops but I am looking for a better way I thought I could manipulate the array_diff method but have had no luck.

$dif_keys = array_diff($fields, $headers);
4
  • 2
    If you are only concerned about the keys, you need array_diff_key() instead. Commented Oct 13, 2017 at 19:54
  • I then want to return an error if the keys in $headers array don't match any of the keys in $fields array. But phone and last_name are in both and so matches, you need to explain the criteria better. Commented Oct 13, 2017 at 19:58
  • @AbraCadaver I then want to return an error if there is a key in $headers array doesn't match any of the keys in $fields array. I updated the question so it is clearer Commented Oct 13, 2017 at 20:01
  • That what I thought, so the answer achieves that. Commented Oct 13, 2017 at 20:07

2 Answers 2

3

Two things:

  1. If you want to check if all keys from $headers are present in $fields, use array_diff_key instead of array_diff. array_diff will only compare values.

  2. Change the order of the arguments. The diff functions return the values of the first argument that aren't present in the subsequent arguments, so if it doesn't matter that $fields has some keys that aren't in $headers, you want to put $headers first.

So I think the code you need is:

$diff = array_diff_key($headers, $fields);

You can evaluate $diff as a boolean. (e.g. if ($diff) {...) If it is empty, it will evaluate as false, which means all keys in $headers had corresponding keys in $fields.

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

4 Comments

Ah, I see I was close. Not really but had the right idea that there was an existing function that could take care of it.
I over thunk it :-(
What if the keys in $fields have the a piece of the key for example. $headers will have "ship_state" and $fields will have "state" am I stuck using a loop with if statements? I don't want that instance to return an error or the difference.
Yes, I don't think you'll be able to check for that with a built-in function.
2

Check the count of the intersecting keys and compare with the count of $headers:

if(count(array_intersect_key($headers, $fields)) == count($headers)) {
    echo 'Good';
} else {
    echo 'Bad';
}

So this is returning all keys in $header that are present in $fields and comparing the count, which should be the same.

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.