How can I validate the structure of my PHP arrays? How can I validate the structure of my PHP arrays? arrays arrays

How can I validate the structure of my PHP arrays?


It doesn't exist built in.

Maybe try something like (untested):

array_diff(array_merge_recursive($arrCandidate, $arrModel), $arrModel)


accepted answer make diff based on values, since it's about array structure you dont want to diff values. Insted you should use [array_diff_key()][1]

Function alone is not recursive. It will not work out of box on sample array from question.


Edit after years: Got into similar trouble whit recursive array functions, so there is mine array_diff_recursive, this does not solve original question because it compare values as well, but i believe it can be easily modified and someone can find it useful .

function array_diff_recursive(array $array1, array $array2){    return array_replace_recursive(        array_diff_recursive_one_way($array1, $array2),        array_diff_recursive_one_way($array2, $array1)    );}//end array_diff_recursive()function array_diff_recursive_one_way(array $array1, array $array2){    $ret = array();    foreach ($array1 as $key => $value) {        if (array_key_exists($key, $array2) === TRUE) {            if (is_array($value) === TRUE) {                $recurse = array_diff_recursive_one_way($value, $array2[$key]);                if (count($recurse) > 0) {                    $ret[$key] = $recurse;                }            } elseif (in_array($value, $array2) === FALSE) {                $ret[$key] = $value;            }        } elseif (in_array($value, $array2) === FALSE) {            $ret[$key] = $value;        }    }    return $ret;}//end array_diff_recursive_one_way()```[1]: http://php.net/manual/en/function.array-diff-key.php