0

Ok so I am stuck with this xml in PHP stuff. I have gotten pretty far considering its my first 3 hours into XML all together ever in my entire life.

I am having trouble pulling data from a XML thing that has @ in the name. See below (obviously im not going to post the whole XML thing but u can see how i got there below that.

SimpleXMLElement Object
(
[@attributes] => Array
    (
        [date] => 2010-09
        [reserved] => 6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30
    )

)

How i got there:

echo $this->General_functions->naked($xml->property[0]->availability->month[0]);

General_functions->naked is just a fast function to wrap and print_r around the given attribute.

My question is, HOW do i get the values inside @attributes cause no matter what i try i cant figure it out. Ive searched the web for a good 45 mins with no real answer.

Thanks in advance. David

2 Answers 2

4

You need to use the attributes() method to get the results as another class. So, for example, to get the date attribute:

$myElement->attributes()->date

Also note that it's not a string, it's a SimpleXML attribute. If you want to get its actual value, you need to cast it to string explicitly:

(string)$myElement->attributes()->date
Sign up to request clarification or add additional context in comments.

Comments

1

Access attributes of an element just as you would elements of an array:

(string) $xml->property[0]->availability->month[0]['date']

Edited to add the cast.

1 Comment

Yea this works too. I still need to add (string) to the front of it though. I thought it returned blank page, but that was a typo of mine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.