SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: XML to Array

  1. #1
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,082
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)

    XML to Array

    Take the following:
    Code XML:
    <a>
    <b />
    <c>
      <c1>aha</c1>
        <c1-1>dsfg</c1-1>
        <c1-2>sdfg</c1-2>
      <c2>fsdg</c2>
    </c>
    </a>

    I've been using SimpleXMLElement() to easily access the data in an array LIKE setup. The problem is it is still an object and I have to loop through an assign values to an array myself, and it become problematic for instances such as <c1>.

    Any ideas on how I can can easily and simply convert this to an array, no matter the depth of the XLS tree?

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    925
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    There's a trick with encoding an object to json and then to array using the second parameter set to true in json_decode():
    PHP Code:
    $xmlstr "<a>
    <b />
    <c>
      <c1>aha</c1>
        <c1-1>dsfg</c1-1>
        <c1-2>sdfg</c1-2>
      <c2>fsdg</c2>
    </c>
    </a>"
    ;

    $xml = new SimpleXMLElement($xmlstr);
    $xml_array json_decode(json_encode($xml), true);
    print_r($xml_array); 

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,082
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Ah, beautiful! Thank you!

  4. #4
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,082
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    So it turns out the remote machine that will run this doesn't have JSON installed, and no possibility of it happening. Any other ideas?

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    925
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    How about a function like this:
    PHP Code:
    function toArray($obj) {
        if(
    is_object($obj)) {
            
    $obj = (array) $obj;
        }
        
        if (
    is_array($obj)) {
            
    $new = array();
            foreach(
    $obj as $key => $val) {
                
    $new[$key] = toArray($val);
            }
            
        } else { 
            
    $new $obj;
        }
        
        return 
    $new;
    }


    $xmlstr "<a>
    <b />
    <c>
      <c1>aha</c1>
        <c1-1>dsfg</c1-1>
        <c1-2>sdfg</c1-2>
      <c2>fsdg</c2>
    </c>
    </a>"
    ;

    $xml = new SimpleXMLElement($xmlstr);
    $xml_array toArray($xml);
    print_r($xml_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
  •