Search code examples
phparraysxmlforeachsimplexml

How to loop through SimpleXML Element Key Attributes using PHP


Can't figure out how to loop through SimpleXMLElement Attributes. Try different methods, still without success..

here is xml:

<items>
<main>
<user_id>3370</user_id>
<msg_count>19</msg_count>
</main>
<msg category="/real-estate/residences/riga/" internal_id="51092" sid="Pārdod">
<phones>1111111111</phones>
<email>e@email.com</email>
<images>
<imgs>https://www.website.com/uploads/object/51092/img-0404jpg-62079a4938e47.jpg</imgs>
<imgs>https://www.website.com/uploads/object/51092/img-0405jpg-62079a496185a.jpg</imgs>
<imgs>https://www.website.com/uploads/object/51092/img-0406jpg-62079a494b2a5.jpg</imgs>
</images>
<options>
<opt name="title1" currency="€">299000.00</opt>
<opt name="title2">3</opt>
<opt name="title3">6</opt>
<opt name="title4" currency="m2">550</opt>
<opt name="Platība">226</opt>
<opt name="Iela">Pildas</opt>
<opt name="Nr.">30C-32</opt>
<opt name="Mājas tips">Mūra ēka</opt>
</options>
<text>
<![CDATA[ Lorem ipsum dolor sit amet consectetur adipisicing elit. Minima optio repellendus provident vel necessitatibus culpa vero, molestias aliquam ut. Eaque ad eius accusantium ipsa, molestiae similique blanditiis. Doloribus corrupti sed, ullam iste unde totam tempore? Amet et labore accusamus expedita eveniet minus ducimus quam dolorum quidem facilis quae, totam quas? ]]>
</text>
</msg>
<msg category="/red/white/" internal_id="52690" sid="today">
<phones>000000000000</phones>
<email>e@email.com</email>
<images>
<imgs>https://www.website.com/uploads/object/52690/img-1651jpg-627398a6bdc90.jpg</imgs>
<imgs>https://www.website.com/uploads/object/52690/img-1652jpg-627398a6e12ff.jpg</imgs>
<imgs>https://www.website.com/uploads/object/52690/img-1663jpg-627398b612a3e.jpg</imgs>
<imgs>https://www.website.com/uploads/object/52690/img-1658jpg-627398a4da68f.jpg</imgs>
</images>
<options>
<opt name="title1" currency="€">52000.00</opt>
<opt name="title2">5</opt>
<opt name="title3">3</opt>
<opt name="title4">1</opt>
<opt name="title5">31</opt>
<opt name="title6">xxx</opt>
<opt name="title7" currency="km">10</opt>
<opt name="Nr.">16</opt>
<opt name="type">one</opt>
</options>
<text>
<![CDATA[ Lorem ipsum dolor sit amet consectetur adipisicing elit. Minima optio repellendus provident vel necessitatibus culpa vero, molestias aliquam ut. Eaque ad eius accusantium ipsa, molestiae similique blanditiis. Doloribus corrupti sed, ullam iste unde totam tempore? Amet et labore accusamus expedita eveniet minus ducimus quam dolorum quidem facilis quae, totam quas? ]]>
</text>
</msg>
</items>

here is my php:

<?php
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => "https://www.------.com/xml/ss?id=178&user_id=3370",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => [
"Content-Type: text/xml"
],
]);

$response = curl_exec($curl);
$xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA);
$err = curl_error($curl);
curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {

foreach($xml->msg as $row){

  echo $row->phones. "<br />";
  echo $row->email. "<br />";
  echo $row->text. "<br />";

  foreach ($row->attributes() as $key => $value) {

    echo $key ." : ". $value ."<br />";
  }

  foreach($row->images->imgs as $key => $value){

      echo "<img src=".$value." alt='' width='150px' height='auto'> ";
      //echo $key . $value;
  }
  echo "<br />";
  
  foreach ($row->options->opt->attributes() as $value) {
    
      echo  $value ." : ";

      foreach($row->options->opt as $key => $value){

        echo  $value. "<br />";

    }
  }
}

}
?>

Here is the part where Im stuck:

enter image description here

Need some help, .. enter image description here This is the part I need to loop through () and get something like this (attributes name: value / title1 : € : 299000.00 ) -->

Maybe is some better way to success, ... ::)) Thanks!


Solution

  • If that is really the XML you're using as input, there is an excess closing MSG tag. That might cause some troubles with parsing.

    <items>
      <main>
        <user_id>3370</user_id>
        <msg_count>19</msg_count>
      </main>
    </msg> <!-- THIS ONE -->
    

    And regarding the options, if I understood correctly, you might want to loop through the options just once and inside that to loop through the attributes of the option you're currently in.

      foreach ($row->options->opt as $opt) {
        
          foreach($opt->attributes() as $key => $value){
            echo  $value. ": ";
          }
    
        echo  $opt->__toString() ."<br>";
    
      }