-1

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"
}
}
]
}
}
2
  • 2
    json_decode($str, true) returns array, not object, so you should do $datas['currency']['ALGYO'] Commented Feb 3, 2021 at 8:39
  • When i do that nothing shows up, also i want to call price of ALGYO, how can i call it? Thank you. Commented Feb 3, 2021 at 8:43

2 Answers 2

0

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 />';
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It will also process the whole list of items each time you want 1 item.
0

Although you say you don't want a list of all elements is not what im looking for, if you want to pick out specific ones in your own order it may be easier.

This just extracts the currentExchangeRate sub-array and indexes it by the currency...

$rates = array_column($datas['mainEntity']["itemListElement"],
         "currentExchangeRate", "currency");

echo "ALGYO rate=" . $rates['ALGYO']['price'];
print_r($rates);

gives...

ALGYO rate=26.14
Array
(
    [ALGYO] => Array
        (
            [@type] => UnitPriceSpecification
            [price] => 26.14
            [priceCurrency] => TRY
        )

    [ARZUM] => Array
        (
            [@type] => UnitPriceSpecification
            [price] => 24.74
            [priceCurrency] => TRY
        )

    [ACSEL] => Array
        (
            [@type] => UnitPriceSpecification
            [price] => 22.9
            [priceCurrency] => TRY
        )

    [ADANA] => Array
        (
            [@type] => UnitPriceSpecification
            [price] => 12.19
            [priceCurrency] => TRY
        )

    [ADBGR] => Array
        (
            [@type] => UnitPriceSpecification
            [price] => 8.62
            [priceCurrency] => TRY
        )

    [PAMEL] => Array
        (
            [@type] => UnitPriceSpecification
            [price] => 22.02
            [priceCurrency] => TRY
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.