Jriker1 Posted September 9, 2013 Share Posted September 9, 2013 0down votefavorite I have some json that would like to parse thru without hardcoding the subvalues so others than me can use this as well. Example of the JSON is: {"payout_history":"0", "round_shares":"1816", "workers": { "jbo.5970": { "alive":"1", "hashrate":"1253" }, "jbo.5970cpu": { "alive":"1", "hashrate":"21" }, "jbo.5970-2": { "alive":"1", "hashrate":"1062" } } } So those jbo level items I can directly access by ['workers']['jbo.5970'] however tried like ['workers][0] but nothing was returned. Want to parse thru all the items under workers and then process the array elements in each jbo value. The jbo.* values are just examples, they could be anything so not static. Thoughts? UPDATED INFO: Using the below, I can get the alive and hash rate status of each worker which I am surprised about as I thought it was one level deeper than I'm going to get them. But I can not get the name of the worker itself. Basically want to output the worker name itself, then if alive and how fast it's hashrate is. $wemineltc = file_get_contents("http://localhost/wemineltc.json"); $obj=json_decode($wemineltc,true); foreach ($obj['workers'] as $wrk) { echo $wrk['alive'] . "<br>"; echo $wrk['hashrate'] . "<br>"; } I can also do like $obj['workers']['jbo.5970']['alive'] to get status of a particular worker, however as mentioned I am assuming the workers name is dynamic. Here is an example URL by the way: https://www.wemineltc.com/api?api_key=6fd24db2b31d3982ad5520c009588efe81b1b4cc07e9fcd7904d04434405e3ef Thanks. JR Quote Link to comment https://forums.phpfreaks.com/topic/282022-parse-json-in-php-and-retrieve-object-names/ Share on other sites More sharing options...
Solution AbraCadaver Posted September 9, 2013 Solution Share Posted September 9, 2013 foreach ($obj['workers'] as $name => $wrk) { echo $name . "<br>"; echo $wrk['alive'] . "<br>"; echo $wrk['hashrate'] . "<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/282022-parse-json-in-php-and-retrieve-object-names/#findComment-1448905 Share on other sites More sharing options...
Jriker1 Posted September 10, 2013 Author Share Posted September 10, 2013 As another follow-up what if it goes one level deeper? So want to output a table like: LTC: jbod.5970: 20 LTC: jbod.5970cpu: 30 NVC: jbod.5970: 50 NVC: jbod.5970cpu: 0 NVC: jbod.5970two: 0 "workers": { "ltc": { "jbod.5970": { "hashrate":"20" }, "jbod.5970cpu": { "hashrate":"30" }, }, "nvc": { "jbod.5970": { "hashrate":"50" }, "jbod.5970cpu": { "hashrate":"0" }, "jbod.5970two": { "hashrate":"0" }, } } Thanks. Hoping this will give me that ahh ha moment and all will become clear. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/282022-parse-json-in-php-and-retrieve-object-names/#findComment-1448986 Share on other sites More sharing options...
vinny42 Posted September 10, 2013 Share Posted September 10, 2013 json_decode( .. , TRUE) will return the JSON string as an array, have you done a var_dump(json_decode($string, TRUE)) to see what you get from the decode? That should give be a nice ah-ha moment... Quote Link to comment https://forums.phpfreaks.com/topic/282022-parse-json-in-php-and-retrieve-object-names/#findComment-1448989 Share on other sites More sharing options...
AbraCadaver Posted September 10, 2013 Share Posted September 10, 2013 foreach ($obj['workers'] as $key => $vals) { foreach($vals as $name => $wrk) { echo "$key : $name : {$wrk['hashrate']}<br>"; } } Something like that. Format to your needs. Quote Link to comment https://forums.phpfreaks.com/topic/282022-parse-json-in-php-and-retrieve-object-names/#findComment-1448990 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.