如何在 PHP 文件中获取此 JSON 数据并将其设为变量?

How do I take this JSON data in PHP file and make it a variable?

我正在拉网 API 并想获取它给我的值之一,一个数字,并将其分配给一个没有小数点的变量。我使用XMLHttprequest获取数据,然后通过

获取个体值
$JSON = file_get_contents("url.to.api.json");
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($JSON, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);

   $character = json_decode($JSON);
print $character->cases/1000; // this gives me the right value, but I can't carry it to other parts of the app or round off the number, I can't figure out how to make it a variable.

我可以打印该值并将其除以一千,但我不确定如何将该值设为变量以及如何删除小数点并向下舍入。如果它给我500.05,我只想要500,我希望能够重复使用这个值。

这是我第一次使用 API 和 JSON。

您可以将值分配给一个变量,然后将它传递给您的代码库,在下面的代码片段中,我将值分配给一个变量并使用 floor() 对其进行舍入:

<?php

$JSON = file_get_contents("url.to.api.json");
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($JSON, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);

$character = json_decode($JSON);
$val = floor($character->cases/1000); // this gives me the right value, but I can't carry it to other parts of the app or round off the number, I can't figure out how to make it a variable.
echo $val;