Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 522603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:22:17+00:00 2026-05-13T08:22:17+00:00

I am trying to determine the end of a foreach loop that is seeded

  • 0

I am trying to determine the end of a foreach loop that is seeded with a collection of DOMNodeList. Currently, I am using a for loop would like to avoid having a ‘magic’ number there. I do know there are only going to be 8 columns, but I would like the code me generic for other applications.

Is it possible to convert this to a Foreach loop? I have tried the end() and next() functions, but they are not returning any data and I suspect that they only work on arrays and not this DOMNodeList collection.

The code is building a CSV file without the trailing ‘,’

Current output is:

“Value 1″,”Value 2″,”Value 3″,”Value 4″,”Value 5″,”Value 6″,”Value 7″,”Value 8”

Here is an example of code:

$cols = $row->getElementsByTagName("td");
$printData = true;

// Throw away the header row
if ($isFirst && $printData) {
   $isFirst = false;
   continue;
}

for ($i = 0; $i <= 8; $i++) {
   $output = iconv("UTF-8", "ASCII//IGNORE", $cols->item($i)->nodeValue);
   $output2 = trim($output);
   if ($i == 8) {
      // Last Column
      echo "\"" . $output2 . "\"" . "\n";
   } else {
      echo "\"" . $output2 . "\"" . ",";
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:22:17+00:00Added an answer on May 13, 2026 at 8:22 am

    You can use:

    $cols->length
    

    To retrieve the number of items in a DOMNodeList.

    See http://php.net/manual/en/class.domnodelist.php

    Edit:
    If you change you’re code to this, you don’t have to worry about the trailing comma, or the length:

    $output = array();
    foreach ($cols as $item) {
       $output = iconv("UTF-8", "ASCII//IGNORE", $item->nodeValue);
       $output2 = trim($output);
    
       $output[] = '"' . $output2 . '"';
    }
    $outputstring = implode(',', $output);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to determine the name of a parent process that launched a
I am trying to determine if a phone is located in this polygon using
I'm trying to determine the iPhone user's location using a CLLocationManager and CLGeocoder. My
I'm trying to determine how to count the matching rows on a table using
Recently I was trying to determine the time needed to calculate a waveform using
I have a VBS file that I am trying to use to determine what
I'm trying to determine the version number of my Silverlight application. Currently I am
I'm trying to determine if I can use a line like: if request.xhr? do
I'm trying to determine the length of a string of characters using JavaScript &
I have a long-running process written in Python 2.7 that I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.