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,

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    Maybe you need to cast the element to string in order to get the value?
    PHP Code:
    $e->addChild('session', (string) $s); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  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?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,604
    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.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  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.

  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.

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