Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How can I assign SimpleXMLElements to variables?

Posted on 2016-11-12
5
Medium Priority
?
104 Views
Last Modified: 2016-11-19
How can I assign SimpleXMLElements to variables?

The following PHP 5.3 code:
#!/usr/local/bin/php
<?php // demo/temp_it_architect.php
/**
 * https://www.experts-exchange.com/questions/28982054/PHP-code-to-retrieve-NDFD-weather-forecasts.html
 *
 * https://www.experts-exchange.com/articles/12239/Introduction-to-Application-Programming-Interfaces.html
 */
error_reporting(E_ALL);

$url = <<<EOD
http://graphical.weather.gov/xml/sample_products/browser_interface/ndfdBrowserClientByDay.php?whichClient=NDFDgenByDay&lat=42.8808333&lon=-85.5228056&listLatLon=&lat1=&lon1=&lat2=&lon2=&resolutionSub=&endPoint1Lat=&endPoint1Lon=&endPoint2Lat=&endPoint2Lon=&centerPointLat=&centerPointLon=&distanceLat=&distanceLon=&resolutionSquare=&zipCodeList=&citiesLevel=&format=24+hourly&startDate=2016-11-12&numDays=7&Unit=e&Submit=Submit
EOD;

// SHOW HOW TO RETRIEVE DATA FROM THE API
$xml = file_get_contents($url);
$obj = SimpleXML_Load_String($xml);
echo '<pre>';
var_dump($obj);

Open in new window

returns weather information that looks like this:
object(SimpleXMLElement)#1 (3) {
  ["@attributes"]=>
  array(1) {
    ["version"]=>
    string(3) "1.0"
  }
  ["head"]=>
  object(SimpleXMLElement)#2 (2) {
    ["product"]=>
    object(SimpleXMLElement)#4 (5) {
      ["@attributes"]=>
      array(3) {
        ["srsName"]=>
        string(8) "WGS 1984"
        ["concise-name"]=>
        string(9) "dwmlByDay"
        ["operational-mode"]=>
        string(8) "official"
      }
      ["title"]=>
      string(58) "NOAA's National Weather Service Forecast by 24 Hour Period"
      ["field"]=>
      string(14) "meteorological"
      ["category"]=>
      string(8) "forecast"
      ["creation-date"]=>
      string(20) "2016-11-13T01:30:38Z"
    }
    ["source"]=>
    object(SimpleXMLElement)#5 (6) {
      ["more-information"]=>
      string(38) "http://www.nws.noaa.gov/forecasts/xml/"
      ["production-center"]=>
      string(37) "Meteorological Development Laboratory"
      ["disclaimer"]=>
      string(39) "http://www.nws.noaa.gov/disclaimer.html"
      ["credit"]=>
      string(23) "http://www.weather.gov/"
      ["credit-logo"]=>
      string(42) "http://www.weather.gov/images/xml_logo.gif"
      ["feedback"]=>
      string(35) "http://www.weather.gov/feedback.php"
    }
  }
  ["data"]=>
  object(SimpleXMLElement)#3 (4) {
    ["location"]=>
    object(SimpleXMLElement)#5 (2) {
      ["location-key"]=>
      string(6) "point1"
      ["point"]=>
      object(SimpleXMLElement)#9 (1) {
        ["@attributes"]=>
        array(2) {
          ["latitude"]=>
          string(5) "42.88"
          ["longitude"]=>
          string(6) "-85.52"
        }
      }
    }
    ["moreWeatherInformation"]=>
    string(75) "http://forecast.weather.gov/MapClick.php?textField1=42.88&textField2=-85.52"
    ["time-layout"]=>
    array(3) {
      [0]=>
      object(SimpleXMLElement)#4 (4) {
        ["@attributes"]=>
        array(2) {
          ["time-coordinate"]=>
          string(5) "local"
          ["summarization"]=>
          string(8) "24hourly"
        }
        ["layout-key"]=>
        string(11) "k-p24h-n7-1"
        ["start-valid-time"]=>
        array(7) {
          [0]=>
          string(25) "2016-11-13T06:00:00-05:00"
          [1]=>
          string(25) "2016-11-14T06:00:00-05:00"
          [2]=>
          string(25) "2016-11-15T06:00:00-05:00"
          [3]=>
          string(25) "2016-11-16T06:00:00-05:00"
          [4]=>
          string(25) "2016-11-17T06:00:00-05:00"
          [5]=>
          string(25) "2016-11-18T06:00:00-05:00"
          [6]=>
          string(25) "2016-11-19T06:00:00-05:00"
        }
        ["end-valid-time"]=>
        array(7) {
          [0]=>
          string(25) "2016-11-14T06:00:00-05:00"
          [1]=>
          string(25) "2016-11-15T06:00:00-05:00"
          [2]=>
          string(25) "2016-11-16T06:00:00-05:00"
          [3]=>
          string(25) "2016-11-17T06:00:00-05:00"
          [4]=>
          string(25) "2016-11-18T06:00:00-05:00"
          [5]=>
          string(25) "2016-11-19T06:00:00-05:00"
          [6]=>
          string(25) "2016-11-20T06:00:00-05:00"
        }
      }
      [1]=>
      object(SimpleXMLElement)#6 (4) {
        ["@attributes"]=>
        array(2) {
          ["time-coordinate"]=>
          string(5) "local"
          ["summarization"]=>
          string(8) "12hourly"
        }
        ["layout-key"]=>
        string(12) "k-p12h-n14-2"
        ["start-valid-time"]=>
        array(14) {
          [0]=>
          string(25) "2016-11-12T18:00:00-05:00"
          [1]=>
          string(25) "2016-11-13T06:00:00-05:00"
          [2]=>
          string(25) "2016-11-13T18:00:00-05:00"
          [3]=>
          string(25) "2016-11-14T06:00:00-05:00"
          [4]=>
          string(25) "2016-11-14T18:00:00-05:00"
          [5]=>
          string(25) "2016-11-15T06:00:00-05:00"
          [6]=>
          string(25) "2016-11-15T18:00:00-05:00"
          [7]=>
          string(25) "2016-11-16T06:00:00-05:00"
          [8]=>
          string(25) "2016-11-16T18:00:00-05:00"
          [9]=>
          string(25) "2016-11-17T06:00:00-05:00"
          [10]=>
          string(25) "2016-11-17T18:00:00-05:00"
          [11]=>
          string(25) "2016-11-18T06:00:00-05:00"
          [12]=>
          string(25) "2016-11-18T18:00:00-05:00"
          [13]=>
          string(25) "2016-11-19T06:00:00-05:00"
        }
        ["end-valid-time"]=>
        array(14) {
          [0]=>
          string(25) "2016-11-13T06:00:00-05:00"
          [1]=>
          string(25) "2016-11-13T18:00:00-05:00"
          [2]=>
          string(25) "2016-11-14T06:00:00-05:00"
          [3]=>
          string(25) "2016-11-14T18:00:00-05:00"
          [4]=>
          string(25) "2016-11-15T06:00:00-05:00"
          [5]=>
          string(25) "2016-11-15T18:00:00-05:00"
          [6]=>
          string(25) "2016-11-16T06:00:00-05:00"
          [7]=>
          string(25) "2016-11-16T18:00:00-05:00"
          [8]=>
          string(25) "2016-11-17T06:00:00-05:00"
          [9]=>
          string(25) "2016-11-17T18:00:00-05:00"
          [10]=>
          string(25) "2016-11-18T06:00:00-05:00"
          [11]=>
          string(25) "2016-11-18T18:00:00-05:00"
          [12]=>
          string(25) "2016-11-19T06:00:00-05:00"
          [13]=>
          string(25) "2016-11-19T18:00:00-05:00"
        }
      }
      [2]=>
      object(SimpleXMLElement)#7 (4) {
        ["@attributes"]=>
        array(2) {
          ["time-coordinate"]=>
          string(5) "local"
          ["summarization"]=>
          string(8) "24hourly"
        }
        ["layout-key"]=>
        string(10) "k-p7d-n1-3"
        ["start-valid-time"]=>
        string(25) "2016-11-13T06:00:00-05:00"
        ["end-valid-time"]=>
        string(25) "2016-11-20T06:00:00-05:00"
      }
    }
    ["parameters"]=>
    object(SimpleXMLElement)#8 (6) {
      ["@attributes"]=>
      array(1) {
        ["applicable-location"]=>
        string(6) "point1"
      }
      ["temperature"]=>
      array(2) {
        [0]=>
        object(SimpleXMLElement)#9 (3) {
          ["@attributes"]=>
          array(3) {
            ["type"]=>
            string(7) "maximum"
            ["units"]=>
            string(10) "Fahrenheit"
            ["time-layout"]=>
            string(11) "k-p24h-n7-1"
          }
          ["name"]=>
          string(25) "Daily Maximum Temperature"
          ["value"]=>
          array(7) {
            [0]=>
            string(2) "55"
            [1]=>
            string(2) "56"
            [2]=>
            string(2) "55"
            [3]=>
            string(2) "56"
            [4]=>
            string(2) "59"
            [5]=>
            string(2) "59"
            [6]=>
            object(SimpleXMLElement)#15 (0) {
            }
          }
        }
        [1]=>
        object(SimpleXMLElement)#10 (3) {
          ["@attributes"]=>
          array(3) {
            ["type"]=>
            string(7) "minimum"
            ["units"]=>
            string(10) "Fahrenheit"
            ["time-layout"]=>
            string(11) "k-p24h-n7-1"
          }
          ["name"]=>
          string(25) "Daily Minimum Temperature"
          ["value"]=>
          array(7) {
            [0]=>
            string(2) "36"
            [1]=>
            string(2) "40"
            [2]=>
            string(2) "40"
            [3]=>
            string(2) "39"
            [4]=>
            string(2) "37"
            [5]=>
            string(2) "48"
            [6]=>
            string(2) "42"
          }
        }
      }
      ["probability-of-precipitation"]=>
      object(SimpleXMLElement)#11 (3) {
        ["@attributes"]=>
        array(3) {
          ["type"]=>
          string(7) "12 hour"
          ["units"]=>
          string(7) "percent"
          ["time-layout"]=>
          string(12) "k-p12h-n14-2"
        }
        ["name"]=>
        string(38) "12 Hourly Probability of Precipitation"
        ["value"]=>
        array(14) {
          [0]=>
          string(1) "0"
          [1]=>
          string(1) "1"
          [2]=>
          string(1) "1"
          [3]=>
          string(1) "3"
          [4]=>
          string(1) "3"
          [5]=>
          string(2) "13"
          [6]=>
          string(1) "7"
          [7]=>
          string(1) "0"
          [8]=>
          string(1) "0"
          [9]=>
          string(1) "0"
          [10]=>
          string(1) "0"
          [11]=>
          string(2) "52"
          [12]=>
          string(2) "43"
          [13]=>
          object(SimpleXMLElement)#15 (0) {
          }
        }
      }
      ["weather"]=>
      object(SimpleXMLElement)#12 (3) {
        ["@attributes"]=>
        array(1) {
          ["time-layout"]=>
          string(11) "k-p24h-n7-1"
        }
        ["name"]=>
        string(37) "Weather Type, Coverage, and Intensity"
        ["weather-conditions"]=>
        array(7) {
          [0]=>
          object(SimpleXMLElement)#15 (1) {
            ["@attributes"]=>
            array(1) {
              ["weather-summary"]=>
              string(17) "Increasing Clouds"
            }
          }
          [1]=>
          object(SimpleXMLElement)#16 (1) {
            ["@attributes"]=>
            array(1) {
              ["weather-summary"]=>
              string(13) "Mostly Cloudy"
            }
          }
          [2]=>
          object(SimpleXMLElement)#17 (1) {
            ["@attributes"]=>
            array(1) {
              ["weather-summary"]=>
              string(13) "Mostly Cloudy"
            }
          }
          [3]=>
          object(SimpleXMLElement)#18 (1) {
            ["@attributes"]=>
            array(1) {
              ["weather-summary"]=>
              string(12) "Partly Sunny"
            }
          }
          [4]=>
          object(SimpleXMLElement)#19 (1) {
            ["@attributes"]=>
            array(1) {
              ["weather-summary"]=>
              string(12) "Partly Sunny"
            }
          }
          [5]=>
          object(SimpleXMLElement)#20 (2) {
            ["@attributes"]=>
            array(1) {
              ["weather-summary"]=>
              string(19) "Chance Rain Showers"
            }
            ["value"]=>
            object(SimpleXMLElement)#22 (1) {
              ["@attributes"]=>
              array(4) {
                ["coverage"]=>
                string(6) "chance"
                ["intensity"]=>
                string(5) "light"
                ["weather-type"]=>
                string(12) "rain showers"
                ["qualifier"]=>
                string(4) "none"
              }
            }
          }
          [6]=>
          object(SimpleXMLElement)#21 (0) {
          }
        }
      }
      ["conditions-icon"]=>
      object(SimpleXMLElement)#13 (3) {
        ["@attributes"]=>
        array(2) {
          ["type"]=>
          string(12) "forecast-NWS"
          ["time-layout"]=>
          string(11) "k-p24h-n7-1"
        }
        ["name"]=>
        string(16) "Conditions Icons"
        ["icon-link"]=>
        array(7) {
          [0]=>
          string(54) "http://www.nws.noaa.gov/weather/images/fcicons/sct.jpg"
          [1]=>
          string(54) "http://www.nws.noaa.gov/weather/images/fcicons/bkn.jpg"
          [2]=>
          string(54) "http://www.nws.noaa.gov/weather/images/fcicons/bkn.jpg"
          [3]=>
          string(54) "http://www.nws.noaa.gov/weather/images/fcicons/sct.jpg"
          [4]=>
          string(54) "http://www.nws.noaa.gov/weather/images/fcicons/sct.jpg"
          [5]=>
          string(61) "http://www.nws.noaa.gov/weather/images/fcicons/hi_shwrs50.jpg"
          [6]=>
          object(SimpleXMLElement)#21 (0) {
          }
        }
      }
      ["hazards"]=>
      object(SimpleXMLElement)#14 (3) {
        ["@attributes"]=>
        array(1) {
          ["time-layout"]=>
          string(10) "k-p7d-n1-3"
        }
        ["name"]=>
        string(33) "Watches, Warnings, and Advisories"
        ["hazard-conditions"]=>
        object(SimpleXMLElement)#21 (0) {
        }
      }
    }
  }
}

Open in new window

How can I assign the "Daily Maximum Temperature" values to strings?
0
Comment
Question by:IT_Architect
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 1000 total points
ID: 41885047
Please see the answer here:
https://www.experts-exchange.com/questions/28982054/PHP-code-to-retrieve-NDFD-weather-forecasts.html#a41883992

See especially $name and $values in the code example.

Please let us know if you still have any questions!
0
 
LVL 17

Assisted Solution

by:Chris Harte
Chris Harte earned 1000 total points
ID: 41886460
In this article simpleXML is used to parse an RSS feed. It also shows how to assign variables from the namespaces and attributes.

https://www.experts-exchange.com/articles/11342/Reading-XML-Namespaces-using-PHP-Without-regex.html
1
 

Author Comment

by:IT_Architect
ID: 41889859
I assigned best solution and assisted solution before, but this morning when I look through them, I had to do some over again.  Let me know if I'm doing something wrong.
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 total points
ID: 41889931
Yeah, E-E has made some "improvements" to the question closing process that have rendered it nearly incomprehensible.  Hope they get this fixed soon!
0
 

Author Closing Comment

by:IT_Architect
ID: 41894387
Tremendous help understanding this.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question