-1

I'm having these two arrays

$expectedArray = [
   'mainKey1' => [
       'subKey1.1' => 'Value 1',
       'subKey1.2' => 'Value 2',
       'subKey1.3' => 'Value 3',
   ],
   'mainKey2' => [
       'subKey2.1' => 'Value 21',
       'subKey2.2' => 'Value 22',
       'subKey2.3' => 'Value 23',
   ],
];

$outputArray = [
   'mainKey1' => [
       'subKey1.1' => 'Value 1',
       'subKey1.2' => 'Value 2',
       'subKey1.3' => 'Value 3',
   ],
   'mainKey2' => [
       'subKey2.1' => 'Value 21',
       'subKey2.2' => 'Value 22',
       'subKey2.3' => 'Value 23',
   ],
];

I'm using the same function as here PHPUnit: assert two arrays are equal, but order of elements not important so

function arrays_are_similar($a, $b) {
  // if the indexes don't match, return immediately
  if (count(array_diff_assoc($a, $b))) {
    return false;
  }
  // we know that the indexes, but maybe not values, match.
  // compare the values between the two arrays
  foreach($a as $k => $v) {
    if ($v !== $b[$k]) {
      return false;
    }
  }
  // we have identical indexes, and no unequal values
  return true;
}

but this returns me an error [PHPUnit\Framework\Exception] Array to string conversion

Any ideas what Im doing wrong?

ltdev
  • 4,037
  • 20
  • 69
  • 129

1 Answers1

1

I don't know what you need the function for (because the order of the elements of both arrays is identical) and also not what you do with PHPUnit. With PHP you can use === to check whether two arrays are identical.

$expectedArray = [
   'mainKey1' => [
       'subKey1.1' => 'Value 1',
       'subKey1.2' => 'Value 2',
       'subKey1.3' => 'Value 3',
   ],
   'mainKey2' => [
       'subKey2.1' => 'Value 21',
       'subKey2.2' => 'Value 22',
       'subKey2.3' => 'Value 23',
   ],
];

$outputArray = [
   'mainKey1' => [
       'subKey1.1' => 'Value 1',
       'subKey1.2' => 'Value 2',
       'subKey1.3' => 'Value 3',
   ],
   'mainKey2' => [
       'subKey2.1' => 'Value 21',
       'subKey2.2' => 'Value 22',
       'subKey2.3' => 'Value 23',
   ],
];

var_dump($outputArray === $expectedArray);  //bool(true)

Check this by changing a little something in an array and you will get bool(false) as result.

jspit
  • 7,276
  • 1
  • 9
  • 17