We have an array with many objects in a style like
<?php
$step = new StepModel(1); $demand = $step->getDemand(); $step2 = $demand->getCustomer(); $demand2 = $step2->getDemand(); ?>
$step and $step2 can be the same objects. So we have an recursive array. Now we need to know if $step == $step2 or $step === $step2. In other words: We need to know the php internal resource ids.
Because there is no function in php api, we made the following function.
Be careful: In our case, all objects have as first attribute ["id":protected]. If your objects are different from this, you need to edit $pattern.
Warning: function is very slow and should only be called if it's necessary for debugging reasons:
<?php
function getObjectInformation(Array $array)
{
ob_start();
var_dump($array);
$dump = ob_get_contents();
ob_end_clean();
$dump = str_replace(' ', '', $dump);
$pattern = '/object\(([a-zA-Z0-9]+)\)#([0-9]+)\([0-9]+\){\\n';
$pattern .= '\["id":protected\]=>\\n';
$pattern .= 'string\([0-9]+\)"([v]?[0-9]+)"/im';
preg_match_all($pattern, $dump, $regs);
array_multisort($regs[1], SORT_ASC, SORT_STRING,
$regs[3], SORT_ASC, SORT_NUMERIC,
$regs[2], SORT_ASC, SORT_NUMERIC);
$lastMatch = array();
$return = array();
for ($i = 0; $i < sizeof($regs[0]); $i ++) {
if (0 == sizeof($lastMatch) ||
$regs[1][$i] != $lastMatch[1] ||
$regs[2][$i] != $lastMatch[2] ||
$regs[3][$i] != $lastMatch[3]) {
array_push($return, array($regs[1][$i],
$regs[2][$i],
$regs[3][$i]));
}
$lastMatch[1] = $regs[1][$i];
$lastMatch[2] = $regs[2][$i];
$lastMatch[3] = $regs[3][$i];
}
return $return;
}
?>
I know, it's not that elegant but I hope it's useful for a few people.