简体   繁体   English

PHP JSON 数组获取值

[英]PHP JSON Array Get Value

I need PHP JSON help.我需要 PHP JSON 帮助。

I have current output :我有当前输出:

{
  "status": 200,
  "response_msec": 15,
  "data": {
    "android": {
      "test1": 15,
      "test2": 6,
      "test3": 15,
      "test4": 101,
      "test5": 87,
      "test6": 8,
      "test9": 119,
      "test10": 101,
      "test11": 107
    }
  }
}

I need print this value : test1 , test2 , test3 ...,test11 .我需要打印这个值: test1 , test2 , test3 ...,test11 .

I have tested some method :我已经测试了一些方法:

$json = json_decode($result, true);
$dec = (Array)json_decode($result);
print_r ($dec["android"]);

and

foreach ($array as $value)
{
   echo $value->android; 
}

But not work.但不工作。

You are missing the ['data'] key,您缺少['data']键,

<?php

$json = '{"status":200,"response_msec":15,"data":{"android":{"test1":15,"test2":6,"test3":15,"test4":101,"test5":87,"test6":8,"test9":119,"test10":101,"test11":107}}}';
$array = json_decode($json, true);
var_dump(array_keys($array['data']['android']));

Check here i have made a php sandbox http://sandbox.onlinephpfunctions.com/code/6f97a9bb499b54919b40d4d12f49049fdd732aef在这里检查我已经做了一个 php 沙箱http://sandbox.onlinephpfunctions.com/code/6f97a9bb499b54919b40d4d12f49049fdd732aef
Also, You can use the function array_keys() to get only the keys of an array, that's what i did.此外,您可以使用函数array_keys()仅获取数组的键,这就是我所做的。

Your code should work if the json string is assigned to $value.如果将 json 字符串分配给 $value,您的代码应该可以工作。 It's just you forgot to include to get the data "data" from "value".只是您忘记包含从“值”中获取数据“数据”。 Your 3rd line of the first method should look like this:第一种方法的第三行应如下所示:
print_r ($dec["data"]["android"]);

Have a great day祝你有美好的一天

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

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