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

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];
}
Sign up to request clarification or add additional context in comments.

2 Comments

I love SimpleXML also for this feature.
@lorenzo-s I "grew up" on DOMDocument, but have mostly switched to SimpleXML because of this ease of use.

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.