1

I'm currently access to an json API. I get response json data like this one.

{
"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"
}

When I'm trying to decode json to get all fileUrl. It's only display the first data in fileUrl[0]. In this case is U2FsdGVkX1-uM6l4RxCWpIiXiCglblQ45V61bTVvuRC2H3ru72DjM0I337iegtL0eG7-t_H_CEeMrc3vopGkVA I think it should have object or array to foreach, not string. How to get all fileUrl in that array?

Here is my code

$fileUrl = json_decode($jsonData)->fileUrl; 
foreach ($fileUrl as $url) {
 print_r($url);
}
4
  • 1
    code you tried???? Commented Mar 18, 2019 at 6:23
  • 1
    Please share your code that used to decode. Commented Mar 18, 2019 at 6:24
  • $fileUrl = json_decode($jsonData)->fileUrl; foreach ($fileUrl as $url) { print_r($url); } Commented Mar 18, 2019 at 6:25
  • you will get all urls as an object in fileUrl variable and if you pass true in json_decode function you will get array of it as per your current code. Commented Mar 18, 2019 at 6:30

2 Answers 2

1

Do it in following way

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

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

working example here

Sign up to request clarification or add additional context in comments.

1 Comment

I've made a change and it works like a charm. Thank you! notepad.pw/c7zc31xw
1

You can try like this:

$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];

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.