1

i am working with an xml, using simplexml and xpath now when i echo a result of xpth query it echoing string but i need to get that string in a array, but when trying to copy in a array it is coming as and simplexml object, like

object(SimpleXMLElement)#237 (1) {
 [0]=>
 string(69) "Hallituksen esitykset uusiksi Yle-laeiksi eduskunnan käsiteltäviksi"
}

is just want "Hallituksen esitykset uusiksi Yle-laeiksi eduskunnan käsiteltäviksi" here is the code,

 $xml = "<?xml version='1.0' encoding='UTF-8' standalone='yes'?>" . $pageBlocks['news'];
 $xmldata = simplexml_load_string($xml);
 $result = $xmldata->xpath('/blocks/block/items/item/strong');
 $feeddata = array();
 while (list(, $node) = each($result)) {
 $feeddata [] = $node[0];
 }
 foreach ($feeddata as $data){
 var_dump($data);
 }

how to solve that

Songo
5,7568 gold badges63 silver badges100 bronze badges
asked May 15, 2012 at 14:01

1 Answer 1

3

To get it as a string, simply cast it via (string), which will internally call SimpleXMLElement's __toString() to return a string representation. That's what's also happening implicitly when you echo it, incidentally.

while (list(, $node) = each($result)) {
 $feeddata [] = (string)$node[0];
}
answered May 15, 2012 at 14:03
2
  • 1
    I love SimpleXML also for this feature. Commented May 15, 2012 at 14:03
  • @lorenzo-s I "grew up" on DOMDocument, but have mostly switched to SimpleXML because of this ease of use. Commented May 15, 2012 at 14:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.