用PHP调用JSON文件,但无法以我希望的方式进行响应

用PHP调用JSON文件,但无法以我希望的方式进行响应,php,arrays,json,json-ld,Php,Arrays,Json,Json Ld,我想用一个非常简单的php文件来跟踪我感兴趣的股票价格。为此,我试图从jsonld文件调用某些数据,但无法这样做。这主要是因为我不能确定正确的顺序。 以下是我的php代码: <? // Get the contents of the JSON file $strJsonFileContents = file_get_contents("data.jsonld"); $datas = json_decode($strJsonFileContents, True); //

我想用一个非常简单的php文件来跟踪我感兴趣的股票价格。为此,我试图从jsonld文件调用某些数据,但无法这样做。这主要是因为我不能确定正确的顺序。 以下是我的php代码:

<?

// 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;

?>

虽然你说你不想要一个所有元素的列表并不是我想要的,但是如果你想按照你自己的顺序选择特定的元素,可能会更容易

这只是提取currentExchangeRate子数组并按货币对其进行索引

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

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

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
        )

)

您可以使用一个函数,该函数允许您从json获取特定于“item”(股票)的数据

您可以(应该)根据需要定制函数,下面是一个搜索商品并返回其价格的示例:

$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 />';
        }
    }
}
$json=file_get_contents('data.json');
$arr=json_decode($json,true);
getItem($arr,'ALGYO');//项目:ALGYO价格:26.14
getItem($arr,'ACSEL');//项目:ACSEL价格:22.9
函数getItem($arr,$item)
{
$records=$arr['maintentity']['itemListElement'];//记录
foreach($记录为$记录){
如果($record['currency']==$item){
回显“项目:”.$item;
回声“
”; 回音“价格:”.$record['currentExchangeRate']['price']; 回声“
”; } } }
json\u decode($str,true)
返回数组,而不是对象,所以您应该执行
$datas['currency']['ALGYO']
当我这样做时,什么都没有显示,我也想调用ALGYO的价格,我该如何调用它?谢谢。更好,更灵活,非常感谢。每次你想要一个项目时,它也会处理整个项目列表。
$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 />';
        }
    }
}