wobsoriano
wobsoriano

Reputation: 13473

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?

Upvotes: 3

Views: 448

Answers (2)

Sujata Halwai
Sujata Halwai

Reputation: 107

First get the response data and print it, see it's array structure using:

echo "<pre>";
print_r($data);
echo "</pre>";

Based on that, access the data you want.

Upvotes: 2

Kraang Prime
Kraang Prime

Reputation: 10509

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":[

      ]
   }
}

Upvotes: 3

Related Questions