0

Hi I have a std class object with objects in it, when I cast it as an array, only the first level is changed to an array. Is there a why to cast the sub-objects as arrays too?

var_dump($heyo);

object(stdClass)#167 (27) { 
    ["uid"]=> object(stdClass)#166 (1) { 
        ["1"]=> int(15)

var_dump((array)($heyo));

array(27) { 
    ["uid"]=> object(stdClass)#166 (1) { 
        ["1"]=> int(15)
2
  • could you show us some sample code? Commented Apr 5, 2012 at 17:08
  • 1
    I would love some more details. How do you cast? What does the object look like? Is it of variable depth, or is it fixed? Commented Apr 5, 2012 at 17:08

1 Answer 1

1

I found this function at if-not-true-then-false.com

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;
    }
}

It'll recursively convert your stdClass objects to arrays

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.