-
Bug
-
Resolution: Fixed
-
Major
-
2.2, 2.3
-
MOODLE_22_STABLE, MOODLE_23_STABLE
-
MOODLE_22_STABLE, MOODLE_23_STABLE
-
wip-
MDL-35001-master -
The function convert_to_array() loses valid data.
For example, it doesn't accept:
array('first' => array('second'), 'third' => array('second'));
|
By passing this array to convert_to_array, it will only return: array('first' => array('second'));
This is caused by the anti-cycle checking. While similar code may work in other languages, in PHP, arrays with the same value can't be differentiated, so you can't check if an array is the same instance and another.
function convert_to_array($var) {
|
$result = array();
|
$references = array();
|
// loop over elements/properties
|
foreach ($var as $key => $value) {
|
// recursively convert objects
|
if (is_object($value) || is_array($value)) {
|
// but prevent cycles
|
if (!in_array($value, $references)) { // PROBLEM HERE
|
$result[$key] = convert_to_array($value);
|
$references[] = $value;
|
}
|
} else {
|
// simple values are untouched
|
$result[$key] = $value;
|
}
|
}
|
return $result;
|
}
|