Home > Backend Development > PHP Tutorial > php解析json的问题

php解析json的问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 14:06:37
Original
907 people have browsed it

	$a = array(array('a','b','c','f','g'),'a'=>123);$jsonStr = json_encode($a);$e = json_decode($jsonStr);
Copy after login

通过print_r打印为:
stdClass Object ( [0] => Array ( [0] => a [1] => b [2] => c [3] => f [4] => g ) [a] => 123 )
如果我想获取123的值我直接$e->a;
但我想获取0里面的值应该怎么处理啊,对象属性名为0肯定是说不同的,这种情况应该怎么取值呢?


回复讨论(解决方案)

echo $e->{'0'}[1]; //b

通常用 $e = json_decode($jsonStr, true); 转换成数组就比较容易处理了
echo $e[0][1]; //b

{} 将其中的内容视为一个整体
这段代码可能有助于你理解

$a = new stdClass;$a->v1 = 'abc';echo $a->v1; //abc$n = 'v1';echo $a->$n; //abcecho $a->{$n}; //abc$i = 1;echo $a->{'v' . $i}; //abc
Copy after login

{} 将其中的内容视为一个整体
这段代码可能有助于你理解PHP code?1234567891011$a = new stdClass;$a->v1 = 'abc'; echo $a->v1; //abc $n = 'v1';echo $a->$n; //abcecho $a->{$n}; //abc $i = 1;echo $a->{'v' . $i}; //abc
……
原来是这样的啊,谢谢版主
$a = array(array('a','b','c','f','g'),'a'=>123);
$jsonStr = json_encode($a);
$e = json_decode($jsonStr);
$i = "0";
$ar =  $e->$i;
这样也可以,呵呵

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template