We help IT Professionals succeed at work.

Receiving "Invalid argument supplied for foreach() " when parsing XML data

Bill Sullivan
I receive "Invalid argument supplied for foreach()" when I am parsing an XML file.  The program goes ahead and parses out the data, but I need to understand the warning, then do what is needed to get rid of it.  My code is below.  It is the second 'foreach' that throws the warning (5th line).

Any help will be much appreciated!

foreach($data->STRUCTURES->ROOF as $roofingItem) {
while($i <= $count) {
      foreach($data->STRUCTURES->ROOF[$i]->LINES->LINE as $lineitem) {
            if($type == "VALLEY") {
                  $valley=$valley + $data->STRUCTURES->ROOF[$i]->LINES->LINE[$ii]['length'];
            if($type == "RIDGE") {
                  $ridge=$ridge + $data->STRUCTURES->ROOF[$i]->LINES->LINE[$ii]['length'];
            if($type == "Eave") {
                  $eave=$eave + $data->STRUCTURES->ROOF[$i]->LINES->LINE[$ii]['length'];
            if($type == "Rake") {
                  $rake=$rake + $data->STRUCTURES->ROOF[$i]->LINES->LINE[$ii]['length'];
            if($type == "Flashing") {
                  $flashing=$flashing + $data->STRUCTURES->ROOF[$i]->LINES->LINE[$ii]['length'];
            if($type == "StepFlash") {
                  $stepflash=$stepflash + $data->STRUCTURES->ROOF[$i]->LINES->LINE[$ii]['length'];
            if($type == "HIP") {
                  $hip=$hip + $data->STRUCTURES->ROOF[$i]->LINES->LINE[$ii]['length'];
Watch Question

Systems architect
Top Expert 2006

This snippet does not show the code.

Where is $count initialised?

There two   foreach() statemens. Which one throws the exception?

Show the complette source code the complette XML and the error message.

ZvonkoSystems architect
Top Expert 2006


Hello Bill, 

please describe what the error was and what was the helpfull solution in my comment or ask EE support to remove this Question and refund your points.