Hey guys,
I've got a recursive function searching an object by index in a multidimensional, associative array.
$settings = array(
'title' => new XCPreferenceShortText('title'),
'html' => new XCPreferenceLongText('html'),
'_blog' => array(
'_blog' => new XCPreferenceHeading('blog'),
'acceptcomments' => new XCPreferenceBoolean('acceptcomments'),
'per_page' => new XCPreferenceNumberSet('per_page')
),
'_publishing' => array(
'_publishing' => new XCPreferenceHeading('publishing'),
'published' => new XCPreferenceBoolean('published'),
'publishon' => new XCPreferenceDate('publishon'),
)
);
The code traverses the array and whenever is_array($value)
returns true, it recurses the whole thing.
function &find($idx, $pref_array = false) {
if ($pref_array === false)
$pref_array = &$this->preferences;
foreach ($pref_array as $key => $data) {
if (is_array($data)) {
$res = $this->find($idx, $data);
if ($res !== false)
return $res;
}
else if ($key == $idx)
return $pref_array[$idx];
}
return false;
}
This function finds (and returns a reference to the result) an object associated to a given key – but when I store the return value / reference in a variable and set that var to null, this has no effect on the actual element in the "original" array.