Radhe Shyam Sharma
Radhe Shyam Sharma

Reputation: 80

How to check multidimensional arrays for the same key pairs ie.exactly same keys but values may differ?

I have made this solution, this is working fine for the normal arrays but failed for multidimensional arrays.

$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r'=>15,'s'=>18]];


function array_equal($a, $b) {
    return (
         is_array($a) 
         && is_array($b) 
         && count($a) == count($b) 
         && array_diff_key($a, $b) === array_diff_key($b, $a)
    );
}


$c = array_equal($a,$b);
echo $c;

For the Above set of arrays it is working fine.

But for the below arrays it returns 1 even if keys are different.

$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r1'=>15,'m'=>18]];

Upvotes: 0

Views: 66

Answers (3)

Progrock
Progrock

Reputation: 7485

I'm reading your question, as that you want to check for the same key structure, but don't care about the values.

I've cheated here by changing all the leaf values to null for both arrays, and then you can compare the leftovers.

<?php

$a = ['a'=>11, 'b'=>2, 'c'=> ['r'=>5, 's'=>8 ]];
$b = ['a'=>1,  'b'=>2, 'c'=> ['r'=>15,'s'=>18]];

function arrays_have_same_keys(array $a, array $b) {
    array_walk_recursive($a, function(&$v) {
        $v = null;
    });
    array_walk_recursive($b, function(&$v) {
        $v = null;
    });

    return $a==$b;
}

var_dump(arrays_have_same_keys($a, $b));

Output:

bool(true)

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31749

This should work -

function array_equal($a, $b) {
    // count mismatch -> not equal
    if (count($a) != count($b)) {
        return false;
    }
    foreach ($a as $key => $val) {
        // key not present -> not equal
        if (empty($b[$key])) {
            return false;
        }
        // check for inner arrays
        if (is_array($val)) {
            return array_equal($val, $b[$key]);
        }
    }
    return true;
}

array_equal($a, $b); // true for first arrays

$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r1'=>15,'m'=>18]];
array_equal($a, $b); // false

Upvotes: 1

xate
xate

Reputation: 6379

This would work if the array keys are in the same order:

https://3v4l.org/jDmON

<?php

function array_keys_recursive(array $array) : array
{
    foreach ($array as $key => $value) {
        if (is_array($value)) {
            $index[$key] = array_keys_recursive($value);
        } else {
            $index []= $key;
        }
    }

    return $index ?? [];
}


$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r'=>15,'s'=>18]];

var_dump(array_keys_recursive($a) === array_keys_recursive($b)); // true

$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'c'=>2, 'b'=>['r'=>15,'s'=>18]];

var_dump(array_keys_recursive($a) === array_keys_recursive($b)); // false

$a = ['a'=>11, 'b'=>2,'c'=>['r'=>5,'s'=>8]];
$b = ['a'=>1, 'b'=>2, 'c'=>['r1'=>15,'m'=>18]];

var_dump(array_keys_recursive($a) === array_keys_recursive($b)); // false

Upvotes: 3

Related Questions