我试图从值数组中显示单个值。
如果我使用print_r($arr)
,则会显示此值
{
"a": 14,
"b": 3,
"c": 61200,
"d": [
"2014-04-22 12:00:06",
"2014-04-23 12:00:06",
"2014-04-24 12:00:06"
]
}
但是当我尝试使用echo $arr->a
和$arr['a']
时。
它显示illegal string offset 'a'
。
如何从值数组中获取单个值?
答案 0 :(得分:2)
看起来像json所以需要首先使用json_decode()
进行解码$d = '{"a":14,"b":3,"c":61200,"d":["2014-04-22 12:00:06","2014-04-23 12:00:06","2014-04-24 12:00:06"]}';
$j = json_decode($d);
echo '<pre>';
print_r($j);
echo $j->a;
答案 1 :(得分:1)
试试这个
$arr = json_decode($arr);
echo $arr->a
答案 2 :(得分:1)
从JSON解码:
$v = json_decode('{
"a": 14,
"b": 3,
"c": 61200,
"d": [
"2014-04-22 12:00:06",
"2014-04-23 12:00:06",
"2014-04-24 12:00:06"
]
}');
echo $v->a;
答案 3 :(得分:1)
输入看起来像JSON - 尝试以下方法来解析JSON数据:
$json_string = '{
"a": 14,
"b": 3,
"c": 61200,
"d": [
"2014-04-22 12:00:06",
"2014-04-23 12:00:06",
"2014-04-24 12:00:06"
]
}';
$vals = json_decode($json_string);
echo $vals->a;
答案 4 :(得分:1)
更多信息会很有用。什么版本的PHP正在使用?您使用的是Web引擎(Apache,Nginx等)还是命令行?如果我错了,请纠正我,但我假设你使用json_decode并将其作为一个对象。
$obj = json_decode('{"a":14,"b":3,"c":61200,"d":["2014-04-22 12:00:06","2014-04-23 12:00:06","2014-04-24 12:00:06"]}');
echo "Result: " . $obj->a;
Result: 14
这在PHP版本5.3,5.4,5.5中运行得很好