dcsimg
www.webdeveloper.com
Results 1 to 6 of 6

Thread: SimpleXML - referencing iterated members

  1. #1
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120

    SimpleXML - referencing iterated members

    Hey folks,

    I'm at my limits on SimpleXML knowledge. Suppose I have two SimpleXMLElements:

    $sessions and $events

    $sessions members basically contain FK's to their parent $event PK's. And I'd like to put add a reference in the each $event member's sessions. My initial thought was something like this:

    PHP Code:
    foreach ($sessions as $s) {
      foreach (
    $events as $e) {
        if ((string)
    $s->parent_id == (string)$e->id) {
          
    $e->addChild('session'$s);
        }
      }

    The code above ultimately adds "blank" children though -- presumably adding a reference to the loop-local $s. And while it seems odd that $s would go out of scope and not $e, the logical solution (in my mind) would be the change the loops:

    PHP Code:
    foreach ($sessions as &$s) {
      foreach (
    $events as &$e) {
        if ((string)
    $s->parent_id == (string)$e->id) {
          
    $e->addChild('session'$s);
        }
      }

    No dice again -- not allowed: Fatal error: An iterator cannot be used with foreach by reference in ...

    Thoughts?


    Thanks,
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,443
    Maybe you need to cast the element to string in order to get the value?
    PHP Code:
    $e->addChild('session', (string) $s); 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  3. #3
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    I guess it didn't even occur to me that addChild explicitly takes a string. Any idea how/if I could add the element from the child structure directly?
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    21,443
    Not sure if SimpleXML can do that. I'm pretty sure the DOM classes can, though probably with more work to iterate through the nodes.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  5. #5
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    I always start off with SimpleXML thinking, oh, I just need to do some simple stuff with XML ...

    Well, thank-you sir.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

  6. #6
    Join Date
    Jan 2007
    Location
    Wisconsin
    Posts
    2,120
    For the curious mind, I ended up copying elements from one tree to another with the following:

    PHP Code:
     function insert_xmlelement(&$simplexml_to$as, &$simplexml_from) {
      
    // add the base child element
      
    $simplexml_temp $simplexml_to->addChild($as, (string)$simplexml_from);
            
      
    // add attributes to the new child
      
    foreach ($simplexml_from->attributes() as $attr_key => $attr_value) {
       
    $simplexml_temp->addAttribute($attr_key$attr_value);
      }
                
      
    // add subchildren
      
    foreach ($simplexml_from as $k => $v) {
       
    insert_xmlelement($simplexml_temp$k$v);
      }
     }  
    // append_simplexml() 
    So, the problem code becomes the following:

    PHP Code:
    foreach ($sessions as $s) {
      foreach (
    $events as $e) {
        if ((string)
    $s->parent_id == (string)$e->id) {
          
    insert_xmlelement($e'session'$s);
        }
      }

    Probably not the most elegant solution. But, it works. (The current parent-child matching "algorithm" is particularly terrible, running in O(n^2) time.)
    Last edited by svidgen; 05-17-2011 at 10:55 AM.
    Jon Wire

    thepointless.com | rounded corner generator

    I agree with Apple. Flash is just terrible.

    Use CODE tags!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles