PHP JSON解析响应

时间:2017-01-31 10:36:38

标签: php arrays json

使用循环我想从给定代码中获取以下paramatersid,device_id

$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}'

请指导如何继续。它似乎是嵌套的json响应

2 个答案:

答案 0 :(得分:1)

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

foreach($arr->response->result->success as $result) {
  $results[] = [
    'paramatersid' => $result->id,
    'deviceid' => $result->device_id
  ];
}
?>

答案 1 :(得分:0)

使用此自定义php代码

depbase=`echo src/gitsh.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
        gcc -DPACKAGE_NAME=\"gitsh\" -DPACKAGE_TARNAME=\"gitsh\" -DPACKAGE_VERSION=\"0.11.1\" -DPACKAGE_STRING=\"gitsh\ 0.11.1\" -DPACKAGE_BUGREPORT=\"hello@thoughtbot.com\" -DPACKAGE_URL=\"\" -DPACKAGE=\"gitsh\" -DVERSION=\"0.11.1\" -I.  -DGITSH_RB_PATH="\"/usr/local/share/gitsh/ruby/gitsh.rb\"" -I/c/dev/readline/include/  -g -O2 -MT src/gitsh.o -MD -MP -MF $depbase.Tpo -c -o src/gitsh.o src/gitsh.c &&\
        mv -f $depbase.Tpo $depbase.Po
src/gitsh.c:1:17: fatal error: err.h: No such file or directory
compilation terminated.
make: *** [src/gitsh.o] Error 1

}';

<?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