1

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;
}

1 Answer 1

1

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));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this helped. I have just flattened the second array, and i asked in foreach if values from post are in_array(), second array. array_diff showed some wierd error :)

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.