0

Using loop I want to get the following paramatersid,device_id from the given code

$str = '{"response":{"success":true,"result":{"success":[{"id":"31281985","device_id":"26965","message":"BIG WINTER CLOSING SALE! Starting from FRIDAY 30th Dec 2016. Discount upto 55%OFF on all Ladies, Gents & Kids Shoes. at All Branches of ENGLISH SHOES MULTAN. ","status":"pending","send_at":1485859669,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":1485863269,"canceled_at":0,"failed_at":0,"received_at":0,"error":"","created_at":1485859669,"contact":{"id":"6317522","name":"923456812536","number":"923456812536"}},{"id":"31281984","device_id":"26965","message":"BIG WINTER CLOSING SALE! Starting from FRIDAY 30th Dec 2016. Discount upto 55%OFF on all Ladies, Gents & Kids Shoes. at All Branches of ENGLISH SHOES MULTAN. ","status":"pending","send_at":1485859669,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":1485863269,"canceled_at":0,"failed_at":0,"received_at":0,"error":"","created_at":1485859669,"contact":{"id":"6317521","name":"923336088811","number":"923336088811"}}],"fails":[]}},"status":200}'

please Guide how to proceed . it seems its nested json response

3
  • 2
    start with: "$obj = json_decode($json_string)" and "var_dump($obj)". Commented Jan 31, 2017 at 10:37
  • please collaborate more . i only want to fetch "id" and device_id Commented Jan 31, 2017 at 10:40
  • Possible duplicate of PHP & Parsing a JSON response Commented Jan 31, 2017 at 10:43

2 Answers 2

1
$arr = json_decode($str);    // decode json result 
$results = [];

foreach($arr->response->result->success as $result) {
  $results[] = [
    'paramatersid' => $result->id,
    'deviceid' => $result->device_id
  ];
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Use this custom php code

<?php

$str = '{
"response": {
    "success": true,
    "result": {
        "success": [{
            "id": "31281087",
            "device_id": "26965",
            "message": "BIG WINTER CLOSING SALE! Starting from FRIDAY 30th Dec 2016. Discount upto 55%OFF on all Ladies, Gents & Kids Shoes. at All Branches of ENGLISH SHOES MULTAN. ",
            "status": "pending",
            "send_at": 1485858313,
            "queued_at": 0,
            "sent_at": 0,
            "delivered_at": 0,
            "expires_at": 1485861913,
            "canceled_at": 0,
            "failed_at": 0,
            "received_at": 0,
            "error": "",
            "created_at": 1485858313,
            "contact": {
                "id": "6317513",
                "name": "0",
                "number": "0"
            }
        }],
        "fails": []
    }
},
"status": 200

}' ;

$arr = json_decode($str);    // decode json result 

//echo "<pre>";
//print_r($arr);   // print $arr value parsed from json

$paramatersid = $arr->response->result->success[0]->id;    // getting id 

$divice_id = $arr->response->result->success[0]->device_id;  // getting device id 

echo $paramatersid ."<br>".$divice_id ;

?>

5 Comments

it gets only 1 item . how to fetch all using loop ?
You have json value , first decode it and after that get value as shown . You can also print decoded json like print_r($arr); ...
i appreciate your response . but what if we need to get more items . i again updated my code and added 2 items in json . please see
You can get all the elements between ' success array at [0] index ' as like this $arr->response->result->success[0]->device_id change device_id to message to get message ...
new code have 2 id . your code is working fine if there is 1 item in json . please have a look on code again .

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.