0

I am trying to check if all required values exist in an array. I thought using array_intersect would be what I should be using:

$required[0] = 'FirstName';
$required[1] = 'LastName';

$posted['Email'] = '[email protected]';
$posted['FirstName'] = 'Bob';
$posted['Group'] = '5';

print_r(array_intersect($required, $posted));

So, I want to be sure that "FirstName" and "LastName" exist in the $posted array. However, "LastName" is missing, but this returns an empty array result.

What am I missing?

2 Answers 2

1

If $posted originally comes from $_POST at some point, (as in the values from inputs on a form), all of the keys will be set, even though some may be set to ''. Since you have keys as values in your $required array, It's probably best to just check the required fields in a loop. You can use empty to simultaneously verify that they exist and have truthy values. Assuming the following code is the body of a function or the contents of a file, something like this should work:

foreach ($required as $requirement) {
    // if everything has to have a value, just return false as soon as something doesn't
    if (empty($posted[$requirement]) return false;
}
return true;

Part of why the way you're doing it with array_intersect isn't working because that function will check the values in $required against the values in $posted, and you need to check the values in $required against the keys in $posted. The other part is that array_intersect will return the values that the two arrays have in common, not the ones that are missing.

If some of the keys in $posted really may not exist, it may be better to define your $required array by key instead of by value, and then use array_diff_key.

$required['FirstName'] = true;
$required['LastName'] = true;

$missing_requirements = array_diff_key($required, $posted);

If every key in $required is present in $posted, the result will be an empty array, which will evaluate to false.

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

2 Comments

Thank you for your answer. I'm surprised that I'm finding that just checking if (empty())... for all the required fields is about 50% faster on average. I thought this would be much faster.
Well, it has the advantage of no function calls, and if you do it like I suggested, you don't actually have to look through everything, you can stop looking as soon as you find one missing thing.
0
print_r(array_intersect($required, array_keys($posted)));

Simple solution:

if (count(array_intersect($required, array_keys($posted))) == count($required) ) { 
return true;
} else {
return false;
}

3 Comments

This returns all of the values in $required that exist in $posted. I basically just need to return true if all exist or false if not.
If (count(array_intersect($required, array_keys($posted))) == count($required) ) { return true;} else {return false;} @JROB
count is no good if you are matching array values that could have different results and the array count gets changed dynamically.

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.