从webservice,我收到一个json对象。
来自json_decode的var_dump($ getUsercodes,true)给了我:
Array(549) {
["Jackson Kim"]=> array(2) { ["codeA"]=> string(1) "x" ["codeB"]=> string(1) "y" }
["Richardson"]=> array(2) { ["codeA"]=> string(1) "x" ["codeB"]=> string(1) "y" }
["Jenson Webb"]=> array(2) { ["codeA"]=> string(1) "x" ["codeB"]=> string(1) "y" }
["Makai Pate"]=> array(2) { ["codeA"]=> string(1) "x" ["codeB"]=> string(1) "y" } ....
相同 - >来自json_decode($ getUsercodes,false)的var_dump给了我:
object(stdClass)#3 (549) {
["Jackson Kim"]=> object(stdClass)#2 (2) { ["codeA"]=> string(1) "x" ["codeB"]=> string(1) "y" }
["Richardson"]=> object(stdClass)#4 (2) { ["codeA"]=> string(1) "x" ["codeB"]=> string(1) "y" }
["Jenson Webb"]=> object(stdClass)#5 (2) { ["codeA"]=> string(1) "x" ["codeB"]=> string(1) "y" }
["Makai Pate"]=> object(stdClass)#6 (2) { ["codeA"]=> string(1) "x" ["codeB"]=> string(1) "y" }
....
如何只打印名字? '杰克逊金'
我试过了:
$result = json_decode ($getUsercodes, true)
echo $result[0]
它给了我未定义的偏移量?
答案 0 :(得分:2)
您可以使用:
$result = json_decode ($getUsercodes, true);
reset($array); // This function reset array index to 1st member.
$first_key = key($array); // This function returns first index of the array where the pointer is.
echo $first_key; // So, this will print the first name
要获得更多密钥:请查看此示例。
$people = array("Peter", "Joe", "Glenn", "Cleveland");
echo current($people) . "<br>"; // Will print 'Peter'
echo next($people) . "<br>"; // Will print 'Joe'
echo next($people) . "<br>"; // Will print 'Glenn'
echo reset($people); // Will print 'Peter'
答案 1 :(得分:0)
这是获取数组
的第一个键值的方法$result = json_decode ($getUsercodes, true)
$first_value = reset($result);