0

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

1 Answer 1

1

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;
}

Php demo

Sign up to request clarification or add additional context in comments.

1 Comment

thanks so much that worked exactly a I had wanted it to. I'm really not sure now how I'd overcomplicated things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.