-1

I want to echo a certain part of json in php. I don't know if the json data is valid or invalid.

I have tried the below code:

$get = get_content('domain');
$json = file_get_contents($get);
$decode = json_decode($json);
$final = $decode->count;
echo $final;

The json data which I get from an URL is: {"15":{"xy":{"cost":6,"count":235}}}

I expect to see only the 235 part. This data keeps on changing. Sometimes the number 6 in the cost can vary as well.

EDIT: Fixed some code. EDIT1: Actually the data was already a string so just used echo substr($get, 30, 3); to achieve the results.

mdhz
  • 113
  • 1
  • 10

1 Answers1

1

Try this, it should work

echo  $decode[15]['xy']['count'];
guillaumepotier
  • 7,369
  • 8
  • 45
  • 72
Vaibhavi S.
  • 1,083
  • 7
  • 20
  • Tried it and there was no output. May be the data from the domain isn't json. If possible can you show how do I get the same data using explode and regex? Thanks. – mdhz Nov 04 '19 at 11:46
  • Please change $arr to $decode. see edited answer – Vaibhavi S. Nov 04 '19 at 11:50
  • Still I couldn't get the output. I have edited the original post to include the domain for testing. Please check it. – mdhz Nov 04 '19 at 12:17
  • 1
    I achieved the output. Actually the data was already a string and not json. I used echo substr($get, 30, 3); to extract the content. – mdhz Nov 04 '19 at 12:36