2

I have an array structure where I want to check if a key/value is present somewhere in the array. But I want to make the test in such a way that I make a an almost mirrored validation array.

Lets say I have a multidimensional array. This is the data I want to validate.

Array
(
[key1] => Array
    (
        [subkey1] => value
        [subkey2] => value
    )

[key2] => Array
    (
        [subkey3] => Array
            (
                [key1] => value
                [key2] => value
                [key3] => value
                [key4] => value
                [key5] => value
                [key6] => value
            )
    )
);

And this is the array of my keys and values that need to be present in the first array.

Array
(
[key1] => Array
    (
        [subkey2] => value
    )

[key2] => Array
    (
        [subkey3] => Array
            (
                [key5] => value
                [key6] => value
            )
    )
);

I cant compare the two arrays because they will never be the same. But I need to run through the data array somehow and validate up against the validation array. Both the key and value need to be at the right place and the key need the same name and the value should be the same as well. I'm just not sure how to make a decent check. Can I make some recursive check? Since some of the keys can be a value or another array it needs to check for this as well... that's why I'm thinking recursive but I'm not sure how to make that.

Hope you can help. Thanks.

7
  • Should "subkey" arrays be considered equal if the keys match or should also all values be matched? Commented May 29, 2013 at 7:51
  • Well is all about the values. But since you need to validate a value the array key needs to be equal as well otherwise the value wont be found. Did that make sence? Commented May 29, 2013 at 7:55
  • I think you need to walk through the array with an recoursive function to check each key and value. I think there is no simpler way. Commented May 29, 2013 at 7:59
  • At least make an example that works; you can't have duplicate keys in arrays declararations. Commented May 29, 2013 at 8:24
  • I was thinking the same thing @steven... any idea how I would go at it? Commented May 29, 2013 at 8:56

2 Answers 2

3

You could use this to recursively determine whether all required keys are present:

function has_recursive($data, $required)
{
    foreach ($required as $key => $value) {
        if (!isset($data[$key])/* && $data[$key] === $value */) {
            return false;
        }
        if (is_array($data[$key]) && false === has_recursive($data[$key], $value)) {
            return false;
        }
    }
    return true;
}

has_recursive($data, $required); // false or true
Sign up to request clarification or add additional context in comments.

3 Comments

Brilliant @Jack - looks very promising. Ill give it a spin.
Great @Jack I have now implemented the solution and added check on values as well. Its working great... I'm not sure how to add the check I have made to your answer. I'm not that familiar with stackoverflow's way of adding such additions.
Yes @Jack pretty much. Just if anyone should find this thread but you pretty much summed it up :) You should have received an up vote now. Thanks for everything!
0

I have not tested it so maybe this doesn't work (if so and this would be no help I will remove the answer).

First method is to check each key-value pairs recursively and then find what matches.

The second option can be that you recursively create flat key - value arrays using array_values() and array_keys(). I mean from something like

array(
  'subkey1' = array(
     'key1' = value1,
     'key2' = value2),
  'subkey2' = value3
)

You iteratively and recursive do

array(
  'key1' = value1,
  'key2' = value2,
  'subkey2' = value3
)

If you do this with both arrays being compared, you can find the matching key-value pairs using array_intersect_assoc().

This would however won't work if for example 'key1' == 'subkey2'.

2 Comments

Good idea @Voitcus My only issue with this is if two subarrays contains the same key with different values. I guess it would override one of the keys. I don't think that's the current case, that the data contains same keys but in order to future proof the code i'm afraid of doing so :S
Yes, there was assumption that there are no duplicate keys in subarrays. In your case it was my first proposal, but marked only and detailed by @Jack. You should then test/improve and probably accept his answer, because only recursive searching seems to be non-sensitive.

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.