0

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]];
  • 1
    So, if you want to check for deeper levels, why haven't you written a recursive function to do so? – Nico Haase Mar 01 '19 at 11:16
  • I am not an expert in Programming right now, i have intermediate level of knowledge so it is bit difficult for me to do so. – Radhe Shyam Sharma Mar 01 '19 at 11:20
  • @RadheShyamSharma What if the keys are the same but in different order? true or false? – Xatenev Mar 01 '19 at 11:20
  • That's not a problem, everybody started somewhere, nobody was born as a professional programmer. Just have a look at some tutorials about recursive functions, you'll definitely learn valuable stuff there – Nico Haase Mar 01 '19 at 11:21
  • Possible duplicate of [How to get all the key in multi-dimensional array in php](https://stackoverflow.com/questions/11234852/how-to-get-all-the-key-in-multi-dimensional-array-in-php) – Nico Haase Mar 01 '19 at 11:22

3 Answers3

3

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
Xatenev
  • 6,383
  • 3
  • 18
  • 42
1

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
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
0

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)
Progrock
  • 7,373
  • 1
  • 19
  • 25