Object and Array questions

I am writing a script that grabs an XML string from a webserver and then tries to parse it. I am using SimpleXML to parse the XML and am running into a problem directly accessing the elements of the array and object.

Here is the code with var_dumps:

print "------------------------------- Report Request Response\n";
$post_data = array( "seq" => "67890" );
$conn = curl_init( $LIST_REPORTS_URL );
$dumb = main_curl ();
$result = curl_exec( $conn );
$xml = new SimpleXMLElement( $result );
curl_close( $conn );
$contents       = $xml->contents;
$reports        = $xml->contents->reports;
$report_array   = (array) $reports->report;
$count          = count ( $report_array );;
print "Number of Reports: $count\n";

foreach ($report_array as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}
print "------------------------------- XML var_dump\n";
var_dump($xml);
print "------------------------------- Reports var_dump\n";
var_dump($reports);
print "------------------------------- Report Array var_dump\n";
var_dump($report_array);

And here is the output from the code:

------------------------------- Report Request Response
Number of Reports: 4
Key: name; Value: 704e26c5-7dba-0cac-3481-66314104333f62d0e23e55f35756<br />
Key: status; Value: completed<br />
Key: readableName; Value: LAN<br />
Key: timestamp; Value: 1385189528<br />
------------------------------- XML var_dump
object(SimpleXMLElement)#2 (3) {
  ["seq"]=>
  string(5) "67890"
  ["status"]=>
  string(2) "OK"
  ["contents"]=>
  object(SimpleXMLElement)#10 (1) {
    ["reports"]=>
    object(SimpleXMLElement)#12 (1) {
      ["report"]=>
      array(3) {
        [0]=>
        object(SimpleXMLElement)#13 (4) {
          ["name"]=>
          string(52) "704e26c5-7dba-0cac-3481-66314104333f62d0e23e55f35756"
          ["status"]=>
          string(9) "completed"
          ["readableName"]=>
          string(3) "LAN"
          ["timestamp"]=>
          string(10) "1385189528"
        }
        [1]=>
        object(SimpleXMLElement)#14 (4) {
          ["name"]=>
          string(52) "237d4645-c39a-fb55-6c78-d1654ff312fa220b62510846ef58"
          ["status"]=>
          string(9) "completed"
          ["readableName"]=>
          string(7) "BOTScan"
          ["timestamp"]=>
          string(10) "1385224078"
        }
        [2]=>
        object(SimpleXMLElement)#15 (4) {
          ["name"]=>
          string(52) "ded003bd-fb8d-c06f-08d5-3c9af762a0d32e4e1930c3833a5d"
          ["status"]=>
          string(9) "completed"
          ["readableName"]=>
          string(8) "BOTScan1"
          ["timestamp"]=>
          string(10) "1385224768"
        }
      }
    }
  }
}
------------------------------- Reports var_dump
object(SimpleXMLElement)#11 (1) {
  ["report"]=>
  array(3) {
    [0]=>
    object(SimpleXMLElement)#10 (4) {
      ["name"]=>
      string(52) "704e26c5-7dba-0cac-3481-66314104333f62d0e23e55f35756"
      ["status"]=>
      string(9) "completed"
      ["readableName"]=>
      string(3) "LAN"
      ["timestamp"]=>
      string(10) "1385189528"
    }
    [1]=>
    object(SimpleXMLElement)#12 (4) {
      ["name"]=>
      string(52) "237d4645-c39a-fb55-6c78-d1654ff312fa220b62510846ef58"
      ["status"]=>
      string(9) "completed"
      ["readableName"]=>
      string(7) "BOTScan"
      ["timestamp"]=>
      string(10) "1385224078"
    }
    [2]=>
    object(SimpleXMLElement)#15 (4) {
      ["name"]=>
      string(52) "ded003bd-fb8d-c06f-08d5-3c9af762a0d32e4e1930c3833a5d"
      ["status"]=>
      string(9) "completed"
      ["readableName"]=>
      string(8) "BOTScan1"
      ["timestamp"]=>
      string(10) "1385224768"
    }
  }
}
------------------------------- Report Array var_dump
array(4) {
  ["name"]=>
  string(52) "704e26c5-7dba-0cac-3481-66314104333f62d0e23e55f35756"
  ["status"]=>
  string(9) "completed"
  ["readableName"]=>
  string(3) "LAN"
  ["timestamp"]=>
  string(10) "1385189528"
}

I am trying to figure out how to directly address each element in the report array, but can only access the first one. (See the foreach section above). Wheter I use (array) or not in $result_array, I still get the correct number of records in the array, I just can’t get the individual array records to come out.

Thanks for the help!

Lorell

it looks like you are assigning only the first one to the report array.

[php]<?php $reports = $xml->contents->reports;
$report_array = (array) $reports->report;// here you are assigning one report?>[/php]

maybe try this foreach instead:

[php]<?php foreach ($reports as $report) {
echo $report->name. “
\n”;
echo $report->status. “
\n”;
}?>[/php]

you can foreach object elements, you don’t need to cast it as an array, you’ll just need to call it differently. check this link:

Thanks for your help. I can’t seem to get that to work either.

Maybe the best way to ask this question is to ask what would the variable name look like to reference each element of this object:

------------------------------- Reports var_dump
object(SimpleXMLElement)#5 (1) {
  ["report"]=>
  array(3) {
    [0]=>
    object(SimpleXMLElement)#4 (4) {
      ["name"]=>
      string(52) "704e26c5-7dba-0cac-3481-66314104333f62d0e23e55f35756"
      ["status"]=>
      string(9) "completed"
      ["readableName"]=>
      string(3) "LAN"
      ["timestamp"]=>
      string(10) "1385189528"
    }
    [1]=>
    object(SimpleXMLElement)#6 (4) {
      ["name"]=>
      string(52) "237d4645-c39a-fb55-6c78-d1654ff312fa220b62510846ef58"
      ["status"]=>
      string(9) "completed"
      ["readableName"]=>
      string(7) "BOTScan"
      ["timestamp"]=>
      string(10) "1385224078"
    }
    [2]=>
    object(SimpleXMLElement)#9 (4) {
      ["name"]=>
      string(52) "ded003bd-fb8d-c06f-08d5-3c9af762a0d32e4e1930c3833a5d"
      ["status"]=>
      string(9) "completed"
      ["readableName"]=>
      string(8) "BOTScan1"
      ["timestamp"]=>
      string(10) "1385224768"
    }
  }
}

Would the variables be the following:

$result->[0]->name $result->[0]->status

etc? Or something else? I see the object $result has an array of three elements. Each element is an object with four elements. Right? Confusing.

Thanks!

Lorell

I’m getting a lot of these errors now that I am trying the above.

PHP Parse error:  syntax error, unexpected '[', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /path/to/script on line 123

After cranking on this for a while I finally understood how to access the array that was inside two layers of XML objects.

Here is the code that did it for me.

        foreach ($xml->contents->reports->report as $report) {
                print "------------------------------------ Report array loop - contents vardump\n";
                echo $report->name. "<br />\n";
                echo $report->status. "<br />\n";
                echo $report->readableName. "<br />\n";
                echo $report->timestamp. "<br />\n";
        }
Sponsor our Newsletter | Privacy Policy | Terms of Service