[英]How can I create a PHP stdClass Object from the output of a PHP stdClass Object
I have the following as a string: 我有以下字符串:
stdClass Object
(
[createResult] => stdClass Object
(
[ReturnCode] => 1
)
)
How can I take the above string and create a new stdClass Object? 如何获取上面的字符串并创建一个新的stdClass对象? I'd like to get the value like this: 我想得到这样的价值:
$rc = $obj->createResults->ReturnCode;
If you can change the way it outputs into something like var_export
, you can afterwards use that string with eval
to get it back. 如果你可以改变它输出到var_export
类的var_export
,你可以在之后使用带有eval
字符串来恢复它。
http://www.php.net/manual/en/function.var-export.php http://www.php.net/manual/en/function.var-export.php
If you can't change it (for whatever reason) and have to use the output of print_r, then you could try this recipe and see if it works for you. 如果您无法更改它(无论出于何种原因)并且必须使用print_r的输出,那么您可以尝试此配方并查看它是否适合您。 http://www.php.net/manual/en/function.print-r.php#93529 http://www.php.net/manual/en/function.print-r.php#93529
However, if at all possible, you should be storing data in a more portable format. 但是,如果可能的话,您应该以更便携的格式存储数据。 You could use php's serialize => unserialize , or json_encode => json_decode , for starters. 您可以使用php的serialize => unserialize或json_encode => json_decode作为初学者。
You can also do it like this: 你也可以这样做:
<?php
$s = "stdClass Object
(
[createResult] => stdClass Object
(
[ReturnCode] => 1
)
)";
$s = preg_match("/\s\[ReturnCode\] => \S+\s/", $s, $m);
echo preg_replace("/\s\[ReturnCode\] => (\S+)\s/", "$1", $m[0]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.