1

I am trying to get rss feed and push the node value to an array. I though my following php codes would create a single array instead of multi-dimensional array.

 //parse rss
    $contents= file_get_contents('http://rss..');
    $xmlStr= simplexml_load_string($contents); 


    $array=array();
          foreach ($xmlStr->item as $node):

                  $array[]=$node->title;

                 echo '<pre>';
                    print_r($array);
                 echo '<pre>';


           endforeach;

but turns out my array output is

Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => App Store Bug Corrupts Binaries; Angry Birds Crash
        )

    [1] => SimpleXMLElement Object
        (
            [0] => In UK, HTC Defeats Apple's "Obvious" Slide Unlock Patent
        )

    [2] => SimpleXMLElement Object
        (
            [0] => WikiLeaks Begins Release of 2.5m Syrian Emails
        )

    [3] => SimpleXMLElement Object
        (
            [0] => A Critical Examination of Bill Gates' Philanthropic Record
        )

    [4] => SimpleXMLElement Object
        (
            [0] => Ask Slashdot: How Does Your Company Evaluate Your Performance?
        )

    [5] => SimpleXMLElement Object
        (
            [0] => UAV Cameras an Eye In the Sky For Adventurous Filmmakers
        )

    [6] => SimpleXMLElement Object
        (
            [0] => Copyrights To Reach Deep Space
        )

    [7] => SimpleXMLElement Object
        (
            [0] => FDA Approves HIV Home-Use Test Kit
        )

    [8] => SimpleXMLElement Object
        (
            [0] => Texas Scientists Regret Loss of Higgs Boson Quest
        )

    [9] => SimpleXMLElement Object
        (
            [0] => Icelandic MP Claims US Vendetta Against WikiLeaks
        )

    [10] => SimpleXMLElement Object
        (
            [0] => Microsoft's 'Cannibalistic Culture'
        )

    [11] => SimpleXMLElement Object
        (
            [0] => Android 4.1 Jelly Bean Review
        )

)

Any idea how to change this? Thanks a lot.

1 Answer 1

2

You will need to cast each node to a string (it is currently a SimpleXMLElement) in order to get back a plain string in a simple array, and retrieve the first item from the title[] array.

SimpleXML implements __toString() magic methods on its objects, which is why it will respond the way it does to print_r(), but to use retrieve it as a string you need to cast it as such.

foreach ($xmlStr->item as $node):
   // Cast the first array value to a string
   $array[] = (string)$node->title[0];
   echo '<pre>';
     print_r($array);
   echo '<pre>';
endforeach;
Sign up to request clarification or add additional context in comments.

Comments

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.