I json decoded API’s json data and wanted to convert the object into array . I wrote a class function to do that . And that function is
public function objectToArray($result) {
if (is_object($result)) {
$result = get_object_vars($result);
}
if (is_array($result)) {
return array_map( @$this->objectToArray, $result);
}
else {
// Return array
return $result;
}
}
So the above code didnt work . It only converted ‘first layers’ objects but not that objects array’s object (I am talking about nested arrays and objects) .
But when I declared that outside the class like function objectToArray and changed array_map’s argument to ‘objectToArry’ all of a sudden it started to work . How come ? Is there a problem with recurive function inside a class in PHP ?
When you want to use methods of objects as callbacks, you have to pass an array:
PHP would have probably told you that, if you hadn’t suppressed errors with the
@operator.