0

I have this JSON API from a bank that returns something like this when called

{
   "AllBillerListRes":{
      "version":"2.3",
      "resDesc":"Success",
      "resDescTh":"สำเร็จ",
      "resDescLocale":"Success",
      "biller":[
         {
            "id":"226",
            "nameEn":"DTAC Online",
            "nameTh":"ดีแทคออนไลน์",
            "nameLocale":"DTAC Online",
            "nameLocaleList":{
               "en":"DTAC Online",
               "th":"ดีแทคออนไลน์",
               "my":"DTAC Online"
            },
            "iconUrl":"https://226.com"
         },
         {
            "id":"249",
            "nameEn":"Link",
            "nameTh":"Link",
            "nameLocale":"Link",
            "nameLocaleList":{
               "en":"Link",
               "th":"Link",
               "my":"Link"
            },
            "iconUrl":"https://249.com"
         }
      ]
   }
}   

I want to get nested array data name and iconUrl in a loop but seems like the AllBillerListRes is not an array as it doesn't have [ is there any other method to json_decode or I can do it with something like this

$decoded = json_decode($json_string, true);
$biller = $decoded->AllBillerListRes[0]->biller;
foreach($biller as $biller)
  {
    $name = $biller->nameEn;
    $icon = $biller->iconUrl;
    echo '<br> Name ->' .$name .'<br> IconURL ->' .$icon; 
  }
Asim
  • 33
  • 1
  • 6

1 Answers1

0

It did work with this, the issue was that the AllBillerListRes could not get index but could get the nested array.

foreach($decoded['AllBillerListRes']['biller'] as $bill)
    {
      $name = $bill['nameEn'];
      $icon = $bill['iconUrl'];
      echo '<br> Name ->' .$name .'<br> IconURL ->' .$icon; 
    }
Asim
  • 33
  • 1
  • 6