I have a problem to convert an object stdClass to array. I have tried in this way:
return (array) $booking;
or
return (array) json_decode($booking,true);
or
return (array) json_decode($booking);
The array before the cast is full with one record, after my try to cast it is empty. How to cast / convert it without delete its rows?
array before cast:
array(1) { [0]=> object(stdClass)#23 (36) { ["id"]=> string(1) "2" ["name"]=> string(0) "" ["code"]=> string(5) "56/13" } }
after cast is empty NULL if I try to make a var_dump($booking);
I have also tried this function but always empty:
public function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
return
. And runningjson_decode
on an array seems pretty desperate to me, probably sitting too long in front of the computer and it's now time to take a break?var_dump($booking);
outputsNULL
?var_dump()
before casting. Do not re-use the same variable name btw. if$booking
was something before casting, it should still be that something before casting and not something different afterwards. Differ between input and processing variables otherwise you run into problems that you don't understand any longer what you do there.