-1

Possible Duplicate:
SimpleXML Reading node with a hyphenated name

I parse xml file which contain fields like that:

<offers>
    <offer>
       <type>Vehicle</type>
       <type-id>2</type-id>
       <category>Car</category>
       <category-id>3</category-id>
       ...
    </offer>
    <offer>
       <type>Vehicle</type>
       <type-id>2</type-id>
       <category>Car</category>
       <category-id>3</category-id>
       ...
    </offer>
    ...
</offers>

Use $xml = simplexml_load_file($file); first and after trying to get values in foreach loop I get error "Use of undefined constant id - assumed 'id'" for field contain 'id' as part of them, like 'type-id' or 'category-id'

 foreach($xml->offers->offer as $offer) {
                    echo $offer->type; // WORKS JUST FINE
                echo $offer->type-id; //THIS GIVE ME ERROR                              
              }      

I trying to set ini_set('error_reporting', E_ALL & ~E_NOTICE); but after it field with 'id' return zero instead of value.

Community
  • 1
  • 1
electroid
  • 603
  • 8
  • 20

3 Answers3

6

The name of the variable is "type-id", which cannot be written simply as $type-id, you need to use curly brackets to access it: ${"type-id"}

echo $offer->{"type-id"};
Dutow
  • 5,638
  • 1
  • 30
  • 40
1

Try this one:

foreach($xml->offers->offer as $offer) {
    echo $offer->type;
    $typeId = 'type-id';
    echo $offer->{$typeId};                        
}  
noslone
  • 1,301
  • 10
  • 14
0

you have to access the object/variable using { and }

echo $offer->{type-id};
hank
  • 3,748
  • 1
  • 24
  • 37