вывести значение из php webservice (объект json) - PullRequest
0 голосов
/ 30 апреля 2018

Из веб-сервиса я получаю объект json.

var_dump из json_decode ($ 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" } ....

То же самое -> var_dump из json_decode ($ getUsercodes, false) дает мне:

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]

Это дает мне неопределенное смещение?

Ответы [ 2 ]

0 голосов
/ 30 апреля 2018

Вы можете использовать:

$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'
0 голосов
/ 30 апреля 2018

Вот как вы получите первое значение ключа массива

$result = json_decode ($getUsercodes, true)   
$first_value = reset($result);
...