Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract value from multidimensional array

Hello so I am using the callback feature of https://smsgateway.me and my current code is here:

<?php
include "smsGateway.php";
$smsGateway = new SmsGateway('[email protected]', 'password');

$message = //extract number value from multidimensional array;

$number = "09058789624";
$deviceID = 5495;
$result = $smsGateway->sendMessageToNumber($number, $message, $deviceID);
?>

In the documentation of smsgateway.me here I've used every http POST request and as you can see the parameter contact there says that it's a multidimensional array that contains the ID, Name and Number. Now what I wanted to do is to only get the Number. How can I do that?

like image 926
wobsoriano Avatar asked May 22 '26 19:05

wobsoriano


1 Answers

Since the response is in JSON, you would do something like the following:

$json = json_decode($result);
echo $json->result->success->contact->number;

Of course you should also add error handling, and check to see the object exists, etc.

For reference, I used the documentation outlining the response that is returned when sending a message as specified here: Send Message to Number

The response format (for success):

{
   "success":true,
   "result":{
      "success":[
         {
            "id":"308",
            "device_id":"4",
            "message":"hello world!",
            "status":"pending",
            "send_at":"1414624856",
            "queued_at":"0",
            "sent_at":"0",
            "delivered_at":"0",
            "expires_at":"1414634856",
            "canceled_at":"0",
            "failed_at":"0",
            "received_at":"0",
            "error":"None",
            "created_at":"1414624856",
            "contact":{
               "id":"14",
               "name":"Phyllis Turner",
               "number":"+447791064713"
            }
         }
      ],
      "fails":[

      ]
   }
}
like image 63
Kraang Prime Avatar answered May 25 '26 09:05

Kraang Prime



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!