Menu

#46 Access to array of matched elements

v1.0_(example)
closed
None
1
2025-11-06
2019-07-14
No

Did you change the code so you have to add ->nodes when accessing an array of matched elements? I could have sworn you could simply do foreach($tds as $td) before. Or am I dreaming?

  $html = str_get_html("<p>Hello World</p><p>Foo Bar</p>");
  foreach($html->find("p") as $tds) {
    foreach($tds->nodes as $td)
      printf("%s\n", $td->plaintext);
  }

Discussion

  • Anonymous

    Anonymous - 2019-07-14

    This example code is clearly wrong. Ignore it for the moment being, and I'll updated it as necessary. If not you may close it in a week or so.

     
  • LogMANOriginal

    LogMANOriginal - 2019-09-21

    No, there were no changes like that. In your example the inner loop is not necessary:

    <?php
    include_once 'simple_html_dom.php';
    
    $html = str_get_html("<p>Hello World</p><p>Foo Bar</p>");
    foreach($html->find("p") as $tds) {
        printf("%s\n", $tds->plaintext);
    }
    
    // Hello World
    // Foo Bar
    

    Unless you want to access individual (sub-)entities, there is no reason to use the nodes array.

     
  • LogMANOriginal

    LogMANOriginal - 2019-09-21
    • status: open --> closed
    • assigned_to: LogMANOriginal
     
  • Anonymous

    Anonymous - 2025-11-06

    DELETE THIS REQUEST

     

Log in to post a comment.

MongoDB Logo MongoDB