LMSouq
php-dev Open

How to convert an stdClass to an array?

AL
Alessandro Minoccheri
1 month ago
3 views
Problem Description
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; } }

AI-Generated Solution

Powered by LMSouq AI · GPT-4.1-mini

✓ Solution Ready
Analyzing problem and generating solution…
Was this solution helpful?
Back to Knowledge Base