PHP json_decode不显示数组中的所有数据

时间:2019-03-18 06:22:08

标签: php json

我当前正在访问json API。我得到像这样的响应json数据。

{
"fileName":null,
"fileUrl":[  
   "U2FsdGVkX1-uM6l4RxCWpIiXiCglblQ45V61bTVvuRC2H3ru72DjM0I337iegtL0eG7-t_H_CEeMrc3vopGkVA",
   "U2FsdGVkX19AX2GGMh_dy71pYYNjgI5Dp1doC58zYqbZbjz02KWdVn5BXjiLcOqjLcgHgKFiOvLQXbRMTzVMJUATPQGh-zMmWaUtUJ0gveLvKOaX3FkM33ZrblwISSBZeocZHxIVxOR1jFpUf9HmuQ",
   "U2FsdGVkX1_XwHvpVBapqhs4fw-6SlUcF8U7rXFxdWTBWeKU7mxQwi1rh9Hor4wPLhQub_qFIprhH59iM90CGsiomMuRmv6cfFBrDzIUkyIEUf83-uBin8L6SWGoyZrQpB6jdAt_elGnS3lYp-TUGg",
   "U2FsdGVkX1-dlLo5wNZpACpERA7aJz-hzegQZ7VGl_uSn9fle1ykAud-ax2kqVhYsmDMJoYbogX9--WFFhAkXCoulHX2wGeD1_N1uvoZvd6-B-4EBmwguntVKHZo3BExZCz0OmRG65gBBXCcV7M10g",
   "U2FsdGVkX1-11v5vUHoO1Yx7Pbsz1OgR4PkmFMHBcw2YO6gQanKg6NL6jLUVF1AqOK5nmTAazur7SZtjIRatJ3_NVR7Q4ya7rZbkXdEIslJKp6hpkgASbJ1hUAWKoNgjoxUEs2FclnQyiTDHntlCUQ"
],
"categoryId":"1110"
}

当我尝试解码json以获取所有fileUrl时。它仅显示fileUrl [0]中的第一个数据。在这种情况下, U2FsdGVkX1-uM6l4RxCWpIiXiCglblQ45V61bTVvuRC2H3ru72DjM0I337iegtL0eG7-t_H_CEeMrc3vopGkVA 我认为应该有对象或数组,而不是字符串。如何获取该数组中的所有fileUrl?

这是我的代码

$fileUrl = json_decode($jsonData)->fileUrl; 
foreach ($fileUrl as $url) {
 print_r($url);
}

2 个答案:

答案 0 :(得分:1)

按照以下方式进行操作

$fileUrls = json_decode($jsonData, true)['fileUrl'];

foreach($fileUrls as $url) {
  echo $url."\n\n";
}

工作示例here

答案 1 :(得分:1)

您可以尝试这样:

$json = '{"fileName":null,"fileUrl":["U2FsdGVkX1-uM6l4RxCWpIiXiCglblQ45V61bTVvuRC2H3ru72DjM0I337iegtL0eG7-t_H_CEeMrc3vopGkVA","U2FsdGVkX19AX2GGMh_dy71pYYNjgI5Dp1doC58zYqbZbjz02KWdVn5BXjiLcOqjLcgHgKFiOvLQXbRMTzVMJUATPQGh-zMmWaUtUJ0gveLvKOaX3FkM33ZrblwISSBZeocZHxIVxOR1jFpUf9HmuQ","U2FsdGVkX1_XwHvpVBapqhs4fw-6SlUcF8U7rXFxdWTBWeKU7mxQwi1rh9Hor4wPLhQub_qFIprhH59iM90CGsiomMuRmv6cfFBrDzIUkyIEUf83-uBin8L6SWGoyZrQpB6jdAt_elGnS3lYp-TUGg","U2FsdGVkX1-dlLo5wNZpACpERA7aJz-hzegQZ7VGl_uSn9fle1ykAud-ax2kqVhYsmDMJoYbogX9--WFFhAkXCoulHX2wGeD1_N1uvoZvd6-B-4EBmwguntVKHZo3BExZCz0OmRG65gBBXCcV7M10g","U2FsdGVkX1-11v5vUHoO1Yx7Pbsz1OgR4PkmFMHBcw2YO6gQanKg6NL6jLUVF1AqOK5nmTAazur7SZtjIRatJ3_NVR7Q4ya7rZbkXdEIslJKp6hpkgASbJ1hUAWKoNgjoxUEs2FclnQyiTDHntlCUQ"],"categoryId":"1110"}';

$jsonObj = json_decode($json);

echo $jsonObj->fileUrl[0];