-1

I have an array inside which I have stdclassObjects. I need to convert those stdClassObjects to arrays. Below is the array:

Array
(
    [serial] => #253
    [details] => stdClass Object
        (
            [Department] => stdClass Object
                (
                    [value] => CI DATA CENTER
                )

            [City] => stdClass Object
                (
                    [value] => NYC
                )

        )

    [owner] => Drey
)

Could somebody please assist me?

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
user2509780
  • 121
  • 2
  • 9
  • 3
    http://stackoverflow.com/questions/19495068/convert-stdclass-object-to-array-in-php – Abhik Chakraborty Mar 25 '14 at 14:41
  • This array is different. I tried all the solutions displayed over there. Didn't work. – user2509780 Mar 25 '14 at 14:47
  • Do you want an array back or just the values? Also what have you tried so far? Because the link provided by Abhik should point you in the right direction. – Gerben Jacobs Mar 25 '14 at 14:53
  • I want an array back. I've tried several things. One of them: the function involving get_object_vars. I searched here itself before posting my question. Didn't help. – user2509780 Mar 25 '14 at 14:54
  • [See this answer](http://stackoverflow.com/questions/2476876/how-do-i-convert-an-object-to-an-array/2476954#2476954) – Rahil Wazir Mar 25 '14 at 14:59

2 Answers2

2

The super lazy way is json_decode and json_encode:

$multiDimArray = json_decode(json_encode($multiDimObject), true);

The documentation on json_decode specifies the second parameter being:

assoc
When TRUE, returned objects will be converted into associative arrays.

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
0
function convertStdClassToArray($stdClass) {

    $outputArray = [];

    if (is_array($stdClass) || !empty($stdClass)) {

        foreach ($stdClass as $field => $value) {

            $outputArray[$field] = $this->convertStdClassToArray($value);

        }
    }
    return $outputArray;
}
Brovoker
  • 896
  • 5
  • 16