Search code examples
phpjsontext-parsing

get value from json file via php non array


I'm a beginner in json please help
I'm trying to access value of certain objects from an online published json file via php script and not able to do so following the examples from this forum

<?php
$str = file_get_contents('http://data.companieshouse.gov.uk/doc/company/02050399.json');
$json = json_decode($str, true);
$companyname = $json["primary topic"]["CompanyName"];
print $companyname;
?>

i get the following error

( ! ) Notice: Undefined index: primary topic in C:\wamp\www\json.php on line 4
Call Stack
#   Time    Memory  Function    Location
1   0.0000  244456  {main}( )   ..\json.php:0

I have tried single and double quotes, [0] for array but to no avail


Solution

  • You should have to use primaryTopic :

    $str = file_get_contents('http://data.companieshouse.gov.uk/doc/company/02050399.json');
    $json = json_decode($str, true);
    $companyname = $json["primaryTopic"]["CompanyName"];
    print $companyname;
    

    Output will : ZENITH PRINT (UK) LIMITED