Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/251.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP文件中获取这个JSON数据并使其成为变量?_Php_Json_Api_Jsondecoder - Fatal编程技术网

如何在PHP文件中获取这个JSON数据并使其成为变量?

如何在PHP文件中获取这个JSON数据并使其成为变量?,php,json,api,jsondecoder,Php,Json,Api,Jsondecoder,我正在拉一个web API,希望获取它给我的一个值,一个数字,并将其分配给一个没有小数点的变量。我使用XMLHttprequest获取数据,然后通过 $JSON = file_get_contents("url.to.api.json"); $jsonIterator = new RecursiveIteratorIterator( new RecursiveArrayIterator(json_decode($JSON, TRUE)), RecursiveIteratorIterator::S

我正在拉一个web 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.
我可以打印该值并将其除以1000,但我不确定如何使该值成为变量,以及如何删除小数点并向下舍入。如果它给我500.05,我只想要500,我希望能够重用这个值


这是我第一次使用API和JSON

您只需将值赋给一个变量,然后将其传递给您的代码库,在下面的代码段中,我将值赋给了一个变量并用于向下取整:

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

成功了,我只需要把括号放在下面:val=floor($character->cases/1000);兜圈子。谢谢,非常感谢。很高兴能帮助你!