I was pulling data in from an XML file and it was printing more than the values, whereas I just wanted the values using this code:
<?php $url = 'site.com'; $result = file_get_contents($url, false); if ($result === false) { /* Handle error */ } $output = $result; $xmlObject = simplexml_load_string($output);?> ////As per Paul's code suggestions. I changed `xml` to `xmlObject` <pre><?php echo print_r($xml);?></pre> Gave me this:
SimpleXMLElement Object ( [@attributes] => Array ( [returnVersion] => 2022v5.0 ) [ReturnHeader] [ReturnData] => SimpleXMLElement Object [FromForm] => SimpleXMLElement Object [DataGrp] [DataGrpPerson] => Array ( [0] => SimpleXMLElement Object ( [PersonNm] => Joan Jett [USAddress] => SimpleXMLElement Object ( [AddressLine1Txt] => 0 EAST Main STREET [CityNm] => CITY NAME [StateAbbreviationCd] => STATE NAME [ZIPCd] => ZIP NUMBER ) [TitleTxt] => POSITION TITLE ) [1] => SimpleXMLElement Object ( PersonNm] => Tom Petty [USAddress] => SimpleXMLElement Object ( [AddressLine1Txt] => 1 EAST Main STREET [CityNm] => CITY NAME [StateAbbreviationCd] => STATE NAME [ZIPCd] => ZIP NUMBER ) [TitleTxt] => POSITION TITLE ) [2] => SimpleXMLElement Object ( PersonNm] => Brandi Carlile [USAddress] => SimpleXMLElement Object ( [AddressLine1Txt] => 2 EAST Main STREET [CityNm] => CITY NAME [StateAbbreviationCd] => STATE NAME [ZIPCd] => ZIP NUMBER ) ) This is the Html I am using to display the data
<?php foreach ($xml->ReturnData->FromForm->DataGrp]->DataGrpPerson[0]->PersonNm as $item) { echo print_r($item);}?>, <?php foreach ($xml->ReturnData->FromForm->DataGrp]->DataGrpPerson[0]->TitleTxt as $item) { echo print_r($item);}?> Here is the output (just the names and title)
SimpleXMLElement Object ( [0] => Joan Jett ) 1, SimpleXMLElement Object ( [0] => POSTION TITLE ) 1 SimpleXMLElement Object ( [0] => S Tom Petty ) 1, SimpleXMLElement Object ( [0] => POSTION TITLE ) 1 SimpleXMLElement Object ( [0] => Brandi Carlile ) 1, SimpleXMLElement Object ( [0] => POSTION TITLE ) How can I get just the values?
I had combed through Stackoverflow for days and yet cannot find anyone describing this problem
echo print_rmakes no sense. Use eitherechoorprint_r.