Reputation: 323
I have a foreach loop that returns object data from a .xml file
I'd like to be able to access another element within that object at the same time.
I'm currently returning the first objects names and id but then attempting to add another elements attributes to that I can only return the values from the first object only:
<?php
include 'log.php';
$robot = new SimpleXMLElement($xmlstr);
foreach ($robot->suite->test as $result) {
echo $result['name'], PHP_EOL;
echo $result['id'], PHP_EOL;
$endtime = $robot->suite->test->status;
echo $endtime['endtime'], PHP_EOL;?><br><?php
}?>
XML
<robot>
<suite>
<test id="s1-t1" name="[TC001] TEST 001">
<status status="PASS" endtime="20190905 22:17:11.062"
critical="yes" starttime="20190905 22:17:07.773"></status>
</test>
<test id="s1-t2" name="[TC002] TEST 002">
<status status="PASS" endtime="20190905 22:17:13.099"
critical="yes" starttime="20190905 22:17:07.773"></status>
</test>
</suite>
</robot>
I'm currently returning the first objects values only:
[TC001] TEST 001 s1-t1 20190905 22:17:11.062
[TC002] TEST 002 s1-t2 20190905 22:17:11.062
what I would like to do is to return:
[TC001] TEST 001 s1-t1 20190905 22:17:11.062
[TC002] TEST 002 s1-t2 20190905 22:17:13.099
Upvotes: 0
Views: 392
Reputation: 163217
In your code, instead of directly accessing $robot->suite->test->status
,
from within the loop, you could get the endtime from status:
$endtime = $result->status;
Your code could look like:
foreach ($robot->suite->test as $result) {
echo $result['name'], PHP_EOL;
echo $result['id'], PHP_EOL;
$endtime = $result->status;
echo $endtime['endtime'], PHP_EOL;
}
Upvotes: 1