简体   繁体   中英

PHP JSON Array Get Value

I need PHP JSON help.

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 .

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,

<?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
Also, You can use the function array_keys() to get only the keys of an array, that's what i did.

Your code should work if the json string is assigned to $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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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