Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Missing is a good recursive array checker, so I built my own. It doesn't compare objects a.t.m.
/**
* Compare 2 variables for equality
* @param $v1
* @param $v2
* @param $loc Name of the compared variables
*/
private function recursiveCompare($v1, $v2, $loc = '', $first = true) {
$equal = true;
if (!is_array($v1) || !is_array($v2)) {
if ($v1 !== $v2) {
$this->assertEqual($v1, $v2, "Mismatch in: " . $loc);
$equal = false;
}
} else {
//Compare arrays
$arr1 = $v1;
$arr2 = $v2;
$keys1 = array_keys($arr1);
$keys2 = array_keys($arr2);
$keys = array_merge($keys1, $keys2);
$equal = true;
for ($i = 0; $i < count($keys); $i++) {
$key = $keys[$i];
if (!array_key_exists($key, $arr1) || !array_key_exists($key, $arr1)) {
@$this->assertEqual($arr1[$key], $arr2[$key], "Mismatch in: " . $loc . "[$key]");
$equal = false;
} else {
$this->recursiveCompare($arr1[$key], $arr2[$key], $loc . "[$key]", false);
}
}
}
if ($first && $equal) {
//add equality message
$this->assertTrue(true, 'Match: ' . $loc);
}
}
Comments