Skip to content
Advertisement

Calling a JSON file with PHP but cant echo in a way i would like to

I would like to follow prices of stocks im interested in with a very simple php file. For this im trying to call certain data from a jsonld file, but unable to do so. Thats mostly because i cant set the correct order. Here is my php code:

<?

// Get the contents of the JSON file
$strJsonFileContents = file_get_contents("data.jsonld");
$datas = json_decode($strJsonFileContents, True);
//var_dump($datas); // print array

$dataname = $datas->currency[ALGYO];
$dataprice = $datas->currency[ALGYO]->price;
echo $data;

?>

I’ve called the file, can echo with var_dump.

The Json file can be seen here

What i want to do is to call the currency ALGYO, and its price, then in another table or line call lets say ACSEL and its price and so on. I’ll only call some certain elements so a list of all elements is not what im looking for. Could someone show me the way? Thanks in advance.


for those who dont want to follow pastebin link im also leavong a readiable version of json below.

{"@context": "http://schema.org",
"@type": "WebPage",
"name": "BORSA",
"mainEntity": {
"@type": "ItemList",
"name": "BORSA",
"itemListElement": [

{
"@type": "ExchangeRateSpecification",
"currency": "ALGYO",
"currentExchangeRate": {
"@type": "UnitPriceSpecification",
"price": 26.14,
"priceCurrency": "TRY"
}
},

{
"@type": "ExchangeRateSpecification",
"currency": "ARZUM",
"currentExchangeRate": {
"@type": "UnitPriceSpecification",
"price": 24.74,
"priceCurrency": "TRY"
}
},

{
"@type": "ExchangeRateSpecification",
"currency": "ACSEL",
"currentExchangeRate": {
"@type": "UnitPriceSpecification",
"price": 22.9,
"priceCurrency": "TRY"
}
},

{
"@type": "ExchangeRateSpecification",
"currency": "ADANA",
"currentExchangeRate": {
"@type": "UnitPriceSpecification",
"price": 12.19,
"priceCurrency": "TRY"
}
},

{
"@type": "ExchangeRateSpecification",
"currency": "ADBGR",
"currentExchangeRate": {
"@type": "UnitPriceSpecification",
"price": 8.62,
"priceCurrency": "TRY"
}
},

{
"@type": "ExchangeRateSpecification",
"currency": "PAMEL",
"currentExchangeRate": {
"@type": "UnitPriceSpecification",
"price": 22.02,
"priceCurrency": "TRY"
}
}
]
}
}

Advertisement

Answer

You could use a function, which allows you to get ‘item'(stock) specific data from the json.

You can(should) tailor the function to fit your needs, here’s an example that searches for an item and returns its price:

$json = file_get_contents('data.json');
$arr = json_decode($json, true);

getItem($arr, 'ALGYO'); // item : ALGYO price: 26.14
getItem($arr, 'ACSEL'); // item : ACSEL price: 22.9

function getItem($arr, $item)
{
    $records = $arr['mainEntity']['itemListElement']; // the records
    foreach ($records as $record) {
        if ($record['currency'] == $item) {
            echo 'item : ' . $item;
            echo '<br />';
            echo 'price : ' . $record['currentExchangeRate']['price'];
            echo '<br />';
        }
    }
}
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement