想要使用php从json结果中获取一个值

时间:2016-02-09 19:51:03

标签: php json

我如何才能获得ID值

$ fo =" 11890408&#34 ;;

{"success":true,"result":{"success":[{"id":"11890408","device_id":"17183","message":"okey","status":"pending","send_at":1455046054,"queued_at":0,"sent_at":0,"delivered_at":0,"expires_at":1455049654,"canceled_at":0,"failed_at":0,"received_at":0,"error":"","created_at":1455046054,"contact":{"id":"2522330","name":"923336458112","number":"923336458112"}}],"fails":[]}}

1 个答案:

答案 0 :(得分:1)

将json字符串转换为php数据结构,然后浏览对象和数组的数据树:

$json = '{
  "success":true,
  "result":{
    "success[
      {
        "id":"11890408",
        "device_id":"17183",
        "message":"okey",
        "status":"pending",
        "send_at":1455046054,
        "queued_at":0,
        "sent_at":0,
        "delivered_at":0,
        "expires_at":1455049654,
        "canceled_at":0,
        "failed_at":0,
        "received_at":0,
        "error":"",
        "created_at":1455046054,
        "contact":{
          "id":"2522330",
          "name":"923336458112",
          "number":"923336458112"
        }
      }
    ],
    "fails":[]
  }
}';

$data = json_decode($json);
$fo = $data->result->success[0]->id;