0

I want to select only specified warehouse to update stock. I.E. I want only 99, but every item has different number of warehouses so the code below only works in 99 warehouse in in 3 row:

$quantity_calc = $this_product->stock[2]; 

Here's sample xml:

<product sku="123546" supplier="BOSCH" price="1.71"> <stock warehouse="SAND">2.00</stock> <stock warehouse="44">2.00</stock> <stock warehouse="55">4.00</stock> <stock warehouse="77">2.00</stock> <stock warehouse="88">2.00</stock> <stock warehouse="97">2.00</stock> <stock warehouse="99">2.00</stock> <stock warehouse="33">2.00</stock> </product> 

and print_r($this_product->stock);

[@attributes] => Array ( [warehouse] => SAND ) [0] => 2.00 [1] => 2.00 [2] => 4.00 [3] => 2.00 [4] => 2.00 [5] => 2.00 [6] => 2.00 [7] => 2.00 ) 
2
  • What you really want ? selecting the stock element where warehouse="99" ? Commented Aug 1, 2017 at 16:27
  • Yes, but warehouse "99" is not always in the same place. Commented Aug 1, 2017 at 16:30

1 Answer 1

1

To fetch a part of an DOM use Xpath expressions. SimpleXMLElement::xpath() provides access to it:

$product = new SimpleXMLElement($xml); var_dump( (string)$product->xpath('//stock[@warehouse=99]')[0] ); 

The DOM extension itself has the DOMXpath class for it:

$document = new DOMDocument(); $document->loadXml($xml); $xpath = new DOMXpath($document); var_dump( $xpath->evaluate('string(//stock[@warehouse=99])') ); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.