-1

Greetings Everybody & thanks in advance for your answers. So, my difficulty is this, i found a nice tutorial about a restful web service and i can not manage to modify it to my needs.

The below sample code works like a charm

 function get_price($find) {
    $products = array(
        "test" => 293,
        "test2" => 348,
        "test3" => 267
    );

    foreach ($products as $product => $price) {
        if ($product == $find) {
            return $price;
            break;
        }}}

The $price return runs correctly for each of the above elements of the array which i choose through a simple html form.

Now my code

    function get_tickets($find){
    $xml = simplexml_load_file("products.xml");  

    // Here i want to return some of the xml's elements
   }

and the included xml file

<products>
    <company>
        <name>test</name>
        <tel>test</tel>
    </company>
    <products>
        <product id="1">
            <name>test</name>
            <value>test</value>
            <color>test</color>
        </product>
    </products>
</products>

I want to be able to access all the elements of the xml like the first sample code. Thank you.

Makis
  • 1,214
  • 3
  • 16
  • 40
  • You've asked a very general question. I can perfectly understand that you *want* to able to do this (and then many other things), however please search for this on this website first and/or read the documentation on how simplexml works, for example [this very good article in the manual which explains it: *Basic SimpleXML usage*](http://php.net/manual/en/simplexml.examples-basic.php) – hakre Dec 25 '14 at 18:52
  • And if you allow me an additional comment: there is no need to call `break;` after `return`. If this was part of the tutorial, the tutorial contains very rough code. – hakre Dec 25 '14 at 18:54

1 Answers1

0

The xpath() function should do want you want. It returns an array of elements that match the path given. For example, $xml->xpath("products/products/product") will give you all your product elements.

James Westman
  • 2,680
  • 1
  • 15
  • 20