将 stdClass 转换为数组时出错
An error when cast stdClass to array
当我像这样将 stdClass 转换为数组时:
$a = (array)json_decode('{"0":{}}');
print_r($a);
print_r($a[0]); // This causes error
输出为:
Array
(
[0] => stdClass Object
(
)
)
Notice: Undefined index: 0 in [...][...] on line
3
我该如何解决这个问题?
尝试这样的事情:下面是如何在 php
中使用 json_decod 的说明
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
$t = json_decode($json, true);
echo $t['c'];
说明:
var_dump(json_decode($json)); gives you array like this.
object(stdClass)[2]
public 'a' => int 1
public 'b' => int 2
public 'c' => int 3
public 'd' => int 4
public 'e' => int 5
访问这些值。
$t = json_decode($json, true);
echo $t['a']; // output 1
echo $t['c']; // output 3
当我像这样将 stdClass 转换为数组时:
$a = (array)json_decode('{"0":{}}');
print_r($a);
print_r($a[0]); // This causes error
输出为:
Array
(
[0] => stdClass Object
(
)
)
Notice: Undefined index: 0 in [...][...] on line 3
我该如何解决这个问题?
尝试这样的事情:下面是如何在 php
中使用 json_decod 的说明$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json));
$t = json_decode($json, true);
echo $t['c'];
说明:
var_dump(json_decode($json)); gives you array like this.
object(stdClass)[2]
public 'a' => int 1
public 'b' => int 2
public 'c' => int 3
public 'd' => int 4
public 'e' => int 5
访问这些值。
$t = json_decode($json, true);
echo $t['a']; // output 1
echo $t['c']; // output 3