stdClass to array?

后端 未结 11 1760
情话喂你
情话喂你 2020-12-03 05:05

i have:

stdClass Object
(
    [0] => stdClass Object
        (
            [one] => aaa
            [two] => sss
        )

    [1] => stdClass O         


        
相关标签:
11条回答
  • 2020-12-03 05:44

    Cast it to an array:

    $array = (array)$stdClass;
    
    0 讨论(0)
  • 2020-12-03 05:45

    Essentially, just type cast it:

    $arr = (array)$obj;
    $var = $arr[0];
    

    But read the caveats here.

    0 讨论(0)
  • 2020-12-03 05:45

    If you have an nested array you can used json_encode and json_decode to convert the whole object to an array:

    $result = json_decode(json_encode($source), JSON_OBJECT_AS_ARRAY);
    
    0 讨论(0)
  • 2020-12-03 05:46

    Cast it

    $array = (array) $stdObject;
    
    0 讨论(0)
  • 2020-12-03 05:47

    Cast it into an array. Currently it is not readable to PHP as an array.

    $array = (array)$stdClass;
    
    0 讨论(0)
提交回复
热议问题