how to display this array.i tried sevaral way.does not working.

Array
(
    [0] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401197400
                    [pretty] => 7:00 PM IST on May 27, 2014
                    [day] => 27
                    [month] => 5
                    [year] => 2014
                    [yday] => 146
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => May
                    [monthname_short] => May
                    [weekday_short] => Tue
                    [weekday] => Tuesday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 1
            [high] => Array
                (
                    [fahrenheit] => 88
                    [celsius] => 31
                )

            [low] => Array
                (
                    [fahrenheit] => 78
                    [celsius] => 26
                )

            [conditions] => Chance of a Thunderstorm
            [icon] => chancetstorms
            [icon_url] => http://icons.wxug.com/i/c/k/chancetstorms.gif
            [skyicon] => 
            [pop] => 60
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.11
                    [mm] => 3
                )

            [qpf_night] => Array
                (
                    [in] => 0.11
                    [mm] => 3
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SSW
                    [degrees] => 212
                )

            [avewind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SSW
                    [degrees] => 212
                )

            [avehumidity] => 85
            [maxhumidity] => 0
            [minhumidity] => 0
        )

    [1] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401283800
                    [pretty] => 7:00 PM IST on May 28, 2014
                    [day] => 28
                    [month] => 5
                    [year] => 2014
                    [yday] => 147
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => May
                    [monthname_short] => May
                    [weekday_short] => Wed
                    [weekday] => Wednesday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 2
            [high] => Array
                (
                    [fahrenheit] => 88
                    [celsius] => 31
                )

            [low] => Array
                (
                    [fahrenheit] => 78
                    [celsius] => 26
                )

            [conditions] => Chance of a Thunderstorm
            [icon] => chancetstorms
            [icon_url] => http://icons.wxug.com/i/c/k/chancetstorms.gif
            [skyicon] => 
            [pop] => 60
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.12
                    [mm] => 3
                )

            [qpf_night] => Array
                (
                    [in] => 0.14
                    [mm] => 4
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 13
                    [kph] => 21
                    [dir] => SSW
                    [degrees] => 197
                )

            [avewind] => Array
                (
                    [mph] => 13
                    [kph] => 21
                    [dir] => SSW
                    [degrees] => 197
                )

            [avehumidity] => 84
            [maxhumidity] => 0
            [minhumidity] => 0
        )

    [2] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401370200
                    [pretty] => 7:00 PM IST on May 29, 2014
                    [day] => 29
                    [month] => 5
                    [year] => 2014
                    [yday] => 148
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => May
                    [monthname_short] => May
                    [weekday_short] => Thu
                    [weekday] => Thursday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 3
            [high] => Array
                (
                    [fahrenheit] => 89
                    [celsius] => 32
                )

            [low] => Array
                (
                    [fahrenheit] => 78
                    [celsius] => 26
                )

            [conditions] => Thunderstorm
            [icon] => tstorms
            [icon_url] => http://icons.wxug.com/i/c/k/tstorms.gif
            [skyicon] => 
            [pop] => 80
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.12
                    [mm] => 3
                )

            [qpf_night] => Array
                (
                    [in] => 0.17
                    [mm] => 4
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 11
                    [kph] => 18
                    [dir] => SW
                    [degrees] => 220
                )

            [avewind] => Array
                (
                    [mph] => 11
                    [kph] => 18
                    [dir] => SW
                    [degrees] => 220
                )

            [avehumidity] => 84
            [maxhumidity] => 0
            [minhumidity] => 0
        )

    [3] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401456600
                    [pretty] => 7:00 PM IST on May 30, 2014
                    [day] => 30
                    [month] => 5
                    [year] => 2014
                    [yday] => 149
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => May
                    [monthname_short] => May
                    [weekday_short] => Fri
                    [weekday] => Friday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 4
            [high] => Array
                (
                    [fahrenheit] => 87
                    [celsius] => 31
                )

            [low] => Array
                (
                    [fahrenheit] => 79
                    [celsius] => 26
                )

            [conditions] => Thunderstorm
            [icon] => tstorms
            [icon_url] => http://icons.wxug.com/i/c/k/tstorms.gif
            [skyicon] => 
            [pop] => 80
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.21
                    [mm] => 5
                )

            [qpf_night] => Array
                (
                    [in] => 0.27
                    [mm] => 7
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => WSW
                    [degrees] => 238
                )

            [avewind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => WSW
                    [degrees] => 238
                )

            [avehumidity] => 85
            [maxhumidity] => 0
            [minhumidity] => 0
        )

    [4] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401543000
                    [pretty] => 7:00 PM IST on May 31, 2014
                    [day] => 31
                    [month] => 5
                    [year] => 2014
                    [yday] => 150
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => May
                    [monthname_short] => May
                    [weekday_short] => Sat
                    [weekday] => Saturday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 5
            [high] => Array
                (
                    [fahrenheit] => 86
                    [celsius] => 30
                )

            [low] => Array
                (
                    [fahrenheit] => 79
                    [celsius] => 26
                )

            [conditions] => Thunderstorm
            [icon] => tstorms
            [icon_url] => http://icons.wxug.com/i/c/k/tstorms.gif
            [skyicon] => 
            [pop] => 80
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.24
                    [mm] => 6
                )

            [qpf_night] => Array
                (
                    [in] => 0.15
                    [mm] => 4
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SW
                    [degrees] => 235
                )

            [avewind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SW
                    [degrees] => 235
                )

            [avehumidity] => 85
            [maxhumidity] => 0
            [minhumidity] => 0
        )

    [5] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401629400
                    [pretty] => 7:00 PM IST on June 01, 2014
                    [day] => 1
                    [month] => 6
                    [year] => 2014
                    [yday] => 151
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => June
                    [monthname_short] => Jun
                    [weekday_short] => Sun
                    [weekday] => Sunday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 6
            [high] => Array
                (
                    [fahrenheit] => 87
                    [celsius] => 31
                )

            [low] => Array
                (
                    [fahrenheit] => 78
                    [celsius] => 26
                )

            [conditions] => Thunderstorm
            [icon] => tstorms
            [icon_url] => http://icons.wxug.com/i/c/k/tstorms.gif
            [skyicon] => 
            [pop] => 80
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.18
                    [mm] => 5
                )

            [qpf_night] => Array
                (
                    [in] => 0.12
                    [mm] => 3
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 13
                    [kph] => 21
                    [dir] => SW
                    [degrees] => 234
                )

            [avewind] => Array
                (
                    [mph] => 13
                    [kph] => 21
                    [dir] => SW
                    [degrees] => 234
                )

            [avehumidity] => 86
            [maxhumidity] => 0
            [minhumidity] => 0
        )

    [6] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401715800
                    [pretty] => 7:00 PM IST on June 02, 2014
                    [day] => 2
                    [month] => 6
                    [year] => 2014
                    [yday] => 152
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => June
                    [monthname_short] => Jun
                    [weekday_short] => Mon
                    [weekday] => Monday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 7
            [high] => Array
                (
                    [fahrenheit] => 86
                    [celsius] => 30
                )

            [low] => Array
                (
                    [fahrenheit] => 78
                    [celsius] => 26
                )

            [conditions] => Thunderstorm
            [icon] => tstorms
            [icon_url] => http://icons.wxug.com/i/c/k/tstorms.gif
            [skyicon] => 
            [pop] => 80
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.24
                    [mm] => 6
                )

            [qpf_night] => Array
                (
                    [in] => 0.18
                    [mm] => 5
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SW
                    [degrees] => 232
                )

            [avewind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SW
                    [degrees] => 232
                )

            [avehumidity] => 85
            [maxhumidity] => 0
            [minhumidity] => 0
        )

    [7] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401802200
                    [pretty] => 7:00 PM IST on June 03, 2014
                    [day] => 3
                    [month] => 6
                    [year] => 2014
                    [yday] => 153
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => June
                    [monthname_short] => Jun
                    [weekday_short] => Tue
                    [weekday] => Tuesday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 8
            [high] => Array
                (
                    [fahrenheit] => 87
                    [celsius] => 31
                )

            [low] => Array
                (
                    [fahrenheit] => 79
                    [celsius] => 26
                )

            [conditions] => Thunderstorm
            [icon] => tstorms
            [icon_url] => http://icons.wxug.com/i/c/k/tstorms.gif
            [skyicon] => 
            [pop] => 80
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.15
                    [mm] => 4
                )

            [qpf_night] => Array
                (
                    [in] => 0.06
                    [mm] => 2
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SW
                    [degrees] => 227
                )

            [avewind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SW
                    [degrees] => 227
                )

            [avehumidity] => 84
            [maxhumidity] => 0
            [minhumidity] => 0
        )

    [8] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401888600
                    [pretty] => 7:00 PM IST on June 04, 2014
                    [day] => 4
                    [month] => 6
                    [year] => 2014
                    [yday] => 154
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => June
                    [monthname_short] => Jun
                    [weekday_short] => Wed
                    [weekday] => Wednesday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 9
            [high] => Array
                (
                    [fahrenheit] => 87
                    [celsius] => 31
                )

            [low] => Array
                (
                    [fahrenheit] => 78
                    [celsius] => 26
                )

            [conditions] => Thunderstorm
            [icon] => tstorms
            [icon_url] => http://icons.wxug.com/i/c/k/tstorms.gif
            [skyicon] => 
            [pop] => 80
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.18
                    [mm] => 5
                )

            [qpf_night] => Array
                (
                    [in] => 0.24
                    [mm] => 6
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SW
                    [degrees] => 222
                )

            [avewind] => Array
                (
                    [mph] => 12
                    [kph] => 19
                    [dir] => SW
                    [degrees] => 222
                )

            [avehumidity] => 84
            [maxhumidity] => 0
            [minhumidity] => 0
        )

    [9] => Array
        (
            [date] => Array
                (
                    [epoch] => 1401975000
                    [pretty] => 7:00 PM IST on June 05, 2014
                    [day] => 5
                    [month] => 6
                    [year] => 2014
                    [yday] => 155
                    [hour] => 19
                    [min] => 00
                    [sec] => 0
                    [isdst] => 0
                    [monthname] => June
                    [monthname_short] => Jun
                    [weekday_short] => Thu
                    [weekday] => Thursday
                    [ampm] => PM
                    [tz_short] => IST
                    [tz_long] => Asia/Colombo
                )

            [period] => 10
            [high] => Array
                (
                    [fahrenheit] => 87
                    [celsius] => 31
                )

            [low] => Array
                (
                    [fahrenheit] => 77
                    [celsius] => 25
                )

            [conditions] => Thunderstorm
            [icon] => tstorms
            [icon_url] => http://icons.wxug.com/i/c/k/tstorms.gif
            [skyicon] => 
            [pop] => 80
            [qpf_allday] => Array
                (
                    [in] => 
                    [mm] => 
                )

            [qpf_day] => Array
                (
                    [in] => 0.48
                    [mm] => 12
                )

            [qpf_night] => Array
                (
                    [in] => 0
                    [mm] => 0
                )

            [snow_allday] => Array
                (
                    [in] => 
                    [cm] => 
                )

            [snow_day] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [snow_night] => Array
                (
                    [in] => 0
                    [cm] => 0
                )

            [maxwind] => Array
                (
                    [mph] => 13
                    [kph] => 21
                    [dir] => SW
                    [degrees] => 226
                )

            [avewind] => Array
                (
                    [mph] => 13
                    [kph] => 21
                    [dir] => SW
                    [degrees] => 226
                )

            [avehumidity] => 83
            [maxhumidity] => 0
            [minhumidity] => 0
        )

)

Dani AI

Generated

Short summary and a safe, practical approach that builds on 's iteration: the array is a list of forecast entries. To show only date, icon and high/low values, iterate the top-level items but always guard for missing keys, sanitize any HTML/URLs (the dump may contain an <a> tag for the icon), and choose which temperature unit to show. The snippet below demonstrates a compact, robust renderer and a small helper to pull nested keys safely.

<?php
function get_nested(array $arr, array $keys, $default = null) {
    foreach ($keys as $k) {
        if (!is_array($arr) || !array_key_exists($k, $arr)) return $default;
        $arr = $arr[$k];
    }
    return $arr;
}

foreach ($forecasts as $item) {
    $epoch = get_nested($item, ['date','epoch']);
    $tz = get_nested($item, ['date','tz_long'], date_default_timezone_get());
    $when = $epoch ? (new DateTime("@$epoch"))->setTimezone(new DateTimeZone($tz))->format('Y-m-d g:i A') : 'n/a';

    $rawIcon = get_nested($item, ['icon_url'], '');
    if (preg_match('#href=["\']([^"\']+)["\']#', $rawIcon, $m)) $rawIcon = $m[1];
    $iconImg = filter_var($rawIcon, FILTER_VALIDATE_URL) ? '<img src="'.htmlspecialchars($rawIcon, ENT_QUOTES).'" alt="icon">' : '';

    $high = get_nested($item, ['high','fahrenheit']) ?? get_nested($item, ['high','celsius']) ?? 'n/a';
    $low  = get_nested($item, ['low','fahrenheit'])  ?? get_nested($item, ['low','celsius'])  ?? 'n/a';

    echo "<div class=\"forecast\">{$when} {$iconImg} <span class=\"temps\">{$high}/{$low}</span></div>";
}
?>

Notes and troubleshooting:

  • If the dataset already provides a preformatted string (like pretty), prefer epoch+DateTime for consistent formatting and timezone control.
  • Sanitize any user-visible text with htmlspecialchars() to avoid XSS.
  • If icon_url contains an anchor tag, the regex above extracts the href; otherwise it accepts a raw URL (validated with FILTER_VALIDATE_URL).
  • If a temperature unit is missing, the code falls back to the other unit or n/a.

This keeps the output minimal and resilient; tweak the HTML classes for styling.

Recommended Answers

All 4 Replies

What I infer from this var_dump is that your main array consists of 10 elements,each of which is an associative array(again some of them are associative).So to say,output them,the following may help you(assume $data to be the main 10-element array:

<?php
    $data = Array(....
            /*Whatever you have given,here we assume it is the variable $data*/
            .....);
    foreach($data as $record){
        foreach($record as $key=>$value){
            echo '<h3>'.$key.'</h3>'; //This outputs 'date' for the first element,
                                      //'period' for the second element and so on.
            //Now output the innermost associative array as you wish,
            //either loop through it if it's an array or simply echo
            //if it's a variable.
        }       
    }
?>

worked..thank you

i want to display.date,icon_url,high,low only..how to do this?

    <?php
        $data = Array(....
                /*Whatever you have given,here we assume it is the variable $data*/
                .....);
        foreach($data as $record){
            echo 'Date :'.$record['date']['pretty'].'<br/>';    
            echo 'Icon URL:'.$record['icon_url'].'<br/>';
            echo 'High :'.$record['high']['fahrenheit'].' degrees Fahrenheit<br/>';
            echo 'Low :'.$record['low']['fahrenheit'].' degrees Fahrenheit<br/>';
        }
    ?>

Modify as necessary

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.