php stdClass 到数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11396033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:26:24  来源:igfitidea点击:

stdClass to array?

php

提问by Lucci Fangorci

i have:

我有:

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

    [1] => stdClass Object
        (
            [one] => ddd
            [two] => fff
        )

    [2] => stdClass Object
        (
            [one] => ggg
            [two] => hhh
        )
}

and i mustget this with keys, for example:

必须用钥匙得到这个,例如:

$var = $stdClass[0]; 

but i have error:

但我有错误:

Fatal error: Cannot use object of type stdClass as array in

致命错误:不能使用 stdClass 类型的对象作为数组

Is possible parse this stdClass to array and use this with keys?

是否可以将此 stdClass 解析为数组并将其与键一起使用?

回答by deceze

Cast it to an array:

将其转换为数组:

$array = (array)$stdClass;

回答by Marcus Recck

If you're using json_decodeto convert that JSON string into an object, you can use the second parameter json_decode($string, true)and that will convert the object to an associative array.

如果json_decode要将该 JSON 字符串转换为对象,则可以使用第二个参数json_decode($string, true),这会将对象转换为关联数组。

If not, what everybody else has said and just type cast it

如果没有,其他人都说了什么,只需输入即可

$array = (array) $stdClass;

$array = (array) $stdClass;

回答by Matej Kovac

Your problem is probably solved since asking, but for reference, quick uncle-google answer:

自问以来,您的问题可能已解决,但作为参考,谷歌叔叔快速回答:

function objectToArray($d) {
  if(is_object($d)) {
    $d = get_object_vars($d);
  }
  if(is_array($d)) {
    return array_map(__FUNCTION__, $d); // recursive
  } else {
    return $d;
  }
}

Full article here. Note I'm not associated with the original author in any way.

全文在这里。请注意,我与原作者没有任何关联。

回答by KingCrunch

Cast it

投射它

$array = (array) $stdObject;

回答by Mike Mackintosh

Of course you can typecast, $var = (array) $obj;, but I would suggest ArrayAccessto your class.

当然你可以打字,$var = (array) $obj;但我会建议ArrayAccess你的班级。

By using ArrayAccess, you can then treat your objects and data as if it was an array, or natively as an object.

通过使用ArrayAccess,您可以将您的对象和数据视为一个数组,或者本机将其视为一个对象。

回答by maxhud

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

将其转换为数组。目前它不能作为数组被 PHP 读取。

$array = (array)$stdClass;

回答by dnagirl

Essentially, just type cast it:

本质上,只需键入:

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

But read the caveats here.

但请阅读此处的注意事项。

回答by Michiel

If you have an nested array you can used json_encodeand json_decodeto convert the whole object to an array:

如果你有一个嵌套的数组,你可以使用json_encodejson_decode整个对象转换为数组:

$result = json_decode(json_encode($source), JSON_OBJECT_AS_ARRAY);

回答by Vladimir verleg

This one worked for me, The decoding and encoding makes for a regular array

这个对我有用,解码和编码构成了一个常规数组

$array = json_decode(json_encode($object), True);

$array = json_decode(json_encode($object), True);

回答by Alex Taylor

function load_something () : \stdClass {

    $result = new \stdClass();

    $result->varA   = 'this is the value of varA';
    $result->varB   = 'this is the value of varB';
    $result->varC   = 'this is the value of varC';

    return $result;
}

$result = load_something();

echo ($result instanceof stdClass)?'Object is stdClass':'Object is not stdClass';
echo PHP_EOL;

print_r($result);

//directly extract a variable from stdClass 
echo PHP_EOL . 'varA = ' . ($result->varA);

//convert to array, then extract
$array = (array)$result;
echo PHP_EOL . 'varA = ' . $array['varA'];