简体   繁体   English

如何从返回的JSON数组中获取值

[英]how to get a value from a returned JSON array

If i have: 如果我有:

print_r($request->getRequestVars());

that prints out: 打印出来:

Array ( [n] => Coors [s] => 3 ) 

how would i print out coors? 我将如何打印库尔斯?

echo $request->getRequestVars()->n;

is not working, and i've tried several other things. 无法正常工作,我已经尝试了其他几件事。 I know this is super basic but it's frustrating me 我知道这是超级基础,但这让我感到沮丧

Try: 尝试:

<?php

$var = $request->getRequestVars();
echo $var['n'];

?>

I assume that the method $request->getRequestVars(); 我假设方法$request->getRequestVars(); acutally returns an array. 手动返回一个数组。 So, you would have to do something along the lines of: 因此,您必须按照以下方式进行操作:

$foo = $request->getRequestVars();
echo $foo['n'];

Please note that $request->getRequestVars() returns array not object. 请注意, $request->getRequestVars()返回的数组不是对象。 PHP 5.4 has Function Array Dereferencing if that is what you are running then you can have: 如果您正在运行PHP 5.4具有函数数组取消引用 ,那么您可以:

echo $request->getRequestVars()['n'];

else 其他

$v = $request->getRequestVars();
echo $v['n'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM