Reputation: 245
I have a two, two dimensional arrayw like this. They are dynamicly created so they can have different numbers of arrays inside.
$userInput['shops'] = Array
(
[0] => Array
(
[id] => 9
)
)
and another that look like this:
$userShops = Array
(
[0] => Array
(
[id] => 9
)
[1] => Array
(
[id] => 10
)
)
First array is something that i receve from post, ids of selected shops. Second array shows all ids of shops that user have. How can i test if all values from userInput can be found in array userShops? I use this for validation so i need to see if all values from post matches the real values for user.
I have tried to do it like this but i receve oknot as result, so i think this should be constructed differently, maybe to somehow count matches...You should ignore my code because i think this is bad approach...In short i need to check if all values from first array can be found in second, if not than show an error.
if(isset($userInput['shops']) && is_array($userInput['shops'])){
foreach($userInput['shops'] as $input){
foreach($userShops as $userShop){
if(in_array($input, $userShop)){
print_r('ok');
}
else {
print_r('not'); or show validation error
}
}
}
exit;
}
Upvotes: 1
Views: 50
Reputation: 5151
How about something like this:
function flatten(array $data) {
return array_map(function(array $element) {
return $element['id'];
}, $data);
}
$user = flatten($userInput['shops']);
$shops = flatten($userShops);
$isCovered = empty(array_diff($user, $shops));
Upvotes: 1