0

I'm using Infobip SMS API to send SMSs to single and muliple destinations, but the delivery reports and SMS logs I get are overwhelming to my users, I want to display just part of the report for each SMS sent. Here is a sample of a JSON report my users get, I just need the groupName parts of the report

    {
     "results":[  
      {  
         "bulkId":"bafdeb3d-719b-4cce-8762-54d47b40f3c5",
         "messageId":"07e03aae-fabc-44ad-b1ce-222e14094d70",
         "to":"41793026727",
         "from":"InfoSMS",
         "text":"Test SMS.",
         "sentAt":"2015-02-23T17:41:11.833+0100",
         "doneAt":"2015-02-23T17:41:11.843+0100",
         "smsCount":1,
         "mccmnc":"22801",
         "price":{  
            "pricePerMessage":0.01,
            "currency":"EUR"
         },
         "status":{  
            "groupId":3,
            "groupName":"DELIVERED",
            "id":5,
            "name":"DELIVERED_TO_HANDSET",
            "description":"Message delivered to handset"
         },
         "error":{  
            "groupId":0,
            "groupName":"OK",
            "id":0,
            "name":"NO_ERROR",
            "description":"No Error",
            "permanent":false
         }
      },
      {  
         "bulkId":"06479ba3-5977-47f6-9346-fee0369bc76b",
         "messageId":"1f21d8d7-f306-4f53-9f6e-eddfce9849ea",
         "to":"41793026727",
         "from":"InfoSMS",
         "text":"Test SMS.",
         "sentAt":"2015-02-23T17:40:31.773+0100",
         "doneAt":"2015-02-23T17:40:31.787+0100",
         "smsCount":1,
         "mccmnc":"22801",
         "price":{  
            "pricePerMessage":0.01,
            "currency":"EUR"
         },
         "status":{  
            "groupId":3,
            "groupName":"DELIVERED",
            "id":5,
            "name":"DELIVERED_TO_HANDSET",
            "description":"Message delivered to handset"
         },
         "error":{  
            "groupId":0,
            "groupName":"OK",
            "id":0,
            "name":"NO_ERROR",
            "description":"No Error",
            "permanent":false
         }
      }
   ]
}
msmolcic
  • 6,407
  • 8
  • 32
  • 56
  • Possible duplicate of [php: loop through json array](https://stackoverflow.com/questions/4731242/php-loop-through-json-array) – Mohammad Nov 21 '18 at 10:38
  • I tried the answers from that with a foreach loop but I get an error **Object of class stdClass could not be converted to string** when execution reaches the value **price**, I think because it is another array – Eddy Khalfan Nov 21 '18 at 11:53
  • You need to put `true` in second parameter. `$arr = json_decode($json, true)` that cause convert json string to array. Then loop through `$arr['results']` – Mohammad Nov 21 '18 at 11:57

1 Answers1

0

Here's the final code that worked

foreach ($arr as $key => $jsons){
    foreach ($jsons as $key => $value){
        if($key == 'status'){
            if (is_array($value)){
                foreach ($value as $key => $val){
                    if($key == 'name'){
                        echo $val;
                    }
                }
            }else{
                if($key == 'name'){
                    echo $value;
                }
            }
        }
    }
}