0

I have an xml which is similar to this:

<xml>
<products name="product_list">
<product id="1" name="product_1">Soap</product>
<product id="2" name="product 3">Shampoo</product>
<product id="3" name="product_3">Sponge</product>
</products>
</xml>

I'm trying to import it via php (using simplexml) but I would like show only one row where the id match another variable I've set.

Something like :

echo product where product['id'] = $variable 

But I have no idea how to do that.

Jenz
  • 8,280
  • 7
  • 44
  • 77

1 Answers1

0

You can try something like this:

<?php

$xml = simplexml_load_string('<xml>
<products name="product_list">
<product id="1" name="product_1">Soap</product>
<product id="2" name="product 3">Shampoo</product>
<product id="3" name="product_3">Sponge</product>
</products>
</xml>');

$variable = 2;

foreach ($xml->products->product as $row) {
  if ((int)$row->attributes()->id === $variable) {
    echo ((string)$row);
  } // if
} // foreach

It will show all products with id of 2...

Goran
  • 3,292
  • 2
  • 29
  • 32