php and nested json: how can i access this element?

前端 未结 3 1702
迷失自我
迷失自我 2021-01-02 07:51

Here\'s the json text:

{
\"data\": {
    \"current_condition\": [{
        \"cloudcover\": \"75\",
        \"humidity\": \"63\",
        \"observation_time\"         


        
相关标签:
3条回答
  • 2021-01-02 08:19

    json_decode() with TRUE as second parameter gives you an associative array. But you're currently trying to access it as an object.

    Try the following:

    echo $arr['data']['current_condition'][0]['temp_F'];
    
    0 讨论(0)
  • 2021-01-02 08:31

    That´s not how you access arrays in PHP

    $array['index']="value";
    
    echo $array['index1']['index2']
    

    For your example:

    echo $arr['data']['current_condition'][0]['temp_F']
    
    0 讨论(0)
  • 2021-01-02 08:36

    You can use json to retrieve json result to a variable and then use variable information to display in js.

    $.ajax({
        'type': 'GET',
        'url': 'abc.com,
        'dataType': 'json',
        success: function (data) {
    
        var response = data;
        // alert(response.data.current_condition)  //something like that
        // for (var i = 0; i < response.length; i++) { }
    
        }
    
    0 讨论(0)
提交回复
热议问题