Tuesday 25 February 2014

Filled Under:

how to convert stdclass object to an array php

here you can find that how to conver stdobject to array in php just follow below steps,

Example :
$newarr = objectToArray($arr);
echo "<pre>"; print_r($newarr);
function objectToArray($d) 
{
    if (is_object($d))
    {
        $d = get_object_vars($d);
    }
    if (is_array($d)) 
    {
        return array_map(__FUNCTION__, $d);
    }
    else
    {
        return $d;
    }
}

Output :

here is your stdclassobject array and above function it will be convert in array like below second output,

stdClass Object
(
    [results] => Array
        (
            [0] => stdClass Object
                (
                    [0] => stdClass Object
                        (
                            [long_name] => India
                            [short_name] => IN
                        )
                )
        )
)

second output,
Array
(
    [results] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [long_name] => India
                            [short_name] => IN
                        )

                )
        )
)








0 comments:

Post a Comment