【发布时间】:2014-07-26 08:49:59
【问题描述】:
我有一个带有子数组的数组:
$test = array("hello" => "world", "object" => array("bye" => "world"));
我想把它转换成对象:
$obj = (object) $test;
父数组变成对象,子数组还是数组:
object(stdClass)[1]
public 'hello' => string 'world' (length=5)
public 'object' =>
array (size=1)
'bye' => string 'world' (length=5)
但我想得到这样的东西:
object(stdClass)[1]
public 'hello' => string 'world' (length=5)
public 'object' =>
object(stdClass)[2]
public 'bye' => string 'world' (length=5)
可以通过以下代码实现:
$testObj = json_decode(json_encode($test));
但这是不好的做法。我怎样才能达到这个结果?
【问题讨论】: