SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot Anagram's Avatar
    Join Date
    Feb 2003
    Location
    Norway
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Converting an object to array - heelp!

    Hey good people,


    Short version: I'm loading XML from a source. Then I remove the "<![[CDATA" tags which is wrapped around all tags. Then, I load the xml into an object using simplexml_load_string. After that, I need it into an array. I've been trying to get the following to work:

    PHP Code:
    <?
    $xml 
    simplexml_load_file("http://www.thetimes.co.za/RSS/ST_World_News_01.xml");
    $search = array("<![CDATA[","]]>");
    $replace "";
    // after loading the file, we remove the CDATA
    $xmlcontent =  str_ireplace($search,"",$xml->asXML());
    // Then we can load the XMLobject using simplexml
    $xml_formatted simplexml_load_string($xmlcontent'SimpleXMLElement'LIBXML_NOCDATA);

    function 
    object_2_array($result

        
    $array = array(); 
        foreach (
    $result as $key=>$value
        { 
            if (
    is_object($value) || is_array($value)) 
            { 
                
    $array[$key]=object_2_array($value); 
            } 
            else 
            { 
                
    $array[$key]=$value
            } 
        } 
        return 
    $array
    }

    print_r(object_2_array($xml_formatted));
    Problem is, I only seem to get this result:

    Array ( [article] => Array ( [title] => Array ( ) [artid] => Array ( ) [leadtext] => Array ( ) [bodytext] => Array ( [p] => Array ( ) ) [firstpublished] => Array ( ) [lastedited] => Array ( ) [byline] => Array ( ) [frontpicurl] => Array ( ) [frontpictext] => Array ( ) [frontpicsrc] => Array ( ) [articlepics] => Array ( [pic] => Array ( ) [pictxt] => Array ( ) [picsrc] => Array ( ) ) [sections] => Array ( [sec] => Array ( ) ) ) )

    As you can see there are many "Array" which I believe are empty - they shouldn't be. Any help on this matter? Or is there any other way I can reference to the values of the object? I've tried $xml_formatted['articles']['article']['title'] but it doesn't output anything : (

    Here's a working adress where I have the project on:
    http://test.boostcom.no/~codetest/php/u.php

    Here's the XML file which the program reads from.
    http://www.thetimes.co.za/RSS/ST_World_News_01.xml

    Any help will be very appreciated!
    If pigs could fly, the prize of bacon would reach the sky.

    www.dosspirit.net - Norwegian
    reviews of DOS games

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    There's no need to remove the CDATA tags, SimpleXML can handle them just fine. Also, why do you need to convert the objects into an array?

    PHP Code:
    $sFeed 'http://www.thetimes.co.za/RSS/ST_World_News_01.xml';
    $oXml  = new SimpleXMLElement($sFeedLIBXML_NOCDATATRUE);
    print_r($oXml); 
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Properties must be public for this to work. Unless you are using PHP 5.3 then can use Reflection.
    PHP Code:
    class SomeTest
    {
        public
          
    $varone   'value'
        
    $vartwo   'value'
        
    $varthree 'value';
    }


    function 
    object2array $obj )
    {
        if ( 
    is_array$obj ) || is_object$obj ) )
            return 
    array_map__FUNCTION__, (array)$obj );
        return 
    $obj;
    }

    $o = new SomeTest;
    $o->varone 'cookies!';
    $o->vartwo = new SomeTest;

    var_dumpobject2array$o ) ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can loop through object with foreach() if all properties are public. No need to convert to an array.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •