Extracting Array Keys

I’d like to extract some data from a json file but I’m struggling to get the data I want.

The json data looks like this:

"user_cards": { "1": { "num_owned": "9", "num_used": "0" }, "2": { "num_owned": "0", "num_used": "0" }, "3": { "num_owned": "0", "num_used": "0" }, "4": { "num_owned": "8", "num_used": "0" }

I’d like to output the the data like this:

1
num_owned: 9
num_used: 0
2
num_owned: 0
num_used: 0
3
num_owned: 0
num_used: 0
4
num_owned: 8
num_used: 0

but so far I’ve only been able to output “num_owned” and “num_used” - how can I modify this code to extract the missing data?

[php]<?php
error_reporting(E_ALL|E_STRICT);
ini_set(‘display_errors’, true);

$json = file_get_contents(“TU.json”);
$json = json_decode($json,true);

foreach($json['user_cards'] as $row)

{
foreach($row as $key => $val)
{
echo $key . ': ’ . $val;
echo ‘
’;
}
}
?>[/php]

Any help would be greatly appreciated

[php]<?php
$json = ‘{
“user_cards”:{
“1”:{
“num_owned”:“9”,
“num_used”:“0”
},
“2”:{
“num_owned”:“0”,
“num_used”:“0”
},
“3”:{
“num_owned”:“0”,
“num_used”:“0”
},
“4”:{
“num_owned”:“8”,
“num_used”:“0”
}
}
}’;

$json = json_decode($json, true);
foreach ($json[‘user_cards’] as $k => $v)
{
echo “$k
”;
foreach ($v as $k => $v)
{
echo “$k : $v
”;
}
}
[/php]

Thank you!

Sponsor our Newsletter | Privacy Policy | Terms of Service