3v4l.org

run code in 300+ PHP versions simultaneously
<?php $xmlString = '<HybridSchedule> <matchNumber>1</matchNumber> <scoreBlueFinal>260</scoreBlueFinal> <scoreRedFinal>135</scoreRedFinal> <startTime>2017-03-25T10:30:00</startTime> <Teams> <Team> <station>Red1</station> <teamNumber>6514</teamNumber> </Team> <Team> <station>Red2</station> <teamNumber>1285</teamNumber> </Team> </Teams> </HybridSchedule>'; // If you have the XML as a string in a variable $xml = simplexml_load_string($xmlString); $teamNumber = null; foreach ($xml->Teams->Team as $team) { if ($team->station == 'Red1') { // We need to cast it as an int, or we're going to end up // with a "SimpleXMLElement"-object. $teamNumber = (int) $team->teamNumber; // We found our item, let's stop iterating break; } } echo $teamNumber;
Output for 7.0.0 - 7.0.25, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
6514

preferences:
103 ms | 1468 KiB | 4 Q