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 8968073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:23:18+00:00 2026-06-15T17:23:18+00:00

How do you count the results inside a foreach then store that value in

  • 0

How do you count the results inside a foreach then store that value in a variable to be used on another foreach.

Example. This foreach returns 5 items

foreach ($xml->items as $item) {
  echo "$item->name";
  echo "$item->address"; 
}

Now I want that the foreach above be counted and stored in say $totalitems and be used on another foreach. This second foreach also counts its results and store in $totalitems2. Something like this:

foreach ($xml->items as $item) { //Same source but they will be displayed separately based on a condition.
  echo "$item->name";
  echo "$item->address";
  if_blah_blah_meet_condition_then_break;
}

So basically what I want here is to restrict the total number of items being displayed on both foreach combined. $totalitems and $totalitems2 should have the sum of 8. 8 is the number I want limit the items returned. Doesn’t matter if the other foreach has more items than the other. 3 and 5. 4 and 4. 6 and 2. Etc.

How can I achieve this? Please advice.

  • 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-06-15T17:23:19+00:00Added an answer on June 15, 2026 at 5:23 pm

    Just use the simple iterator ++ methods. When you are on the second foreach, watch for when $i passes the number that you want to stop it.

    Code:

    $i = 0;    
    foreach ($xml->items as $item) {
        echo "$item->name";
        echo "$item->address";
        $i++;
    }
    
    foreach ($xml->items as $item) {
        echo "$item->name";
        echo "$item->address";
    
        $i++;
        if ($i > 5) { // or whatever the number is
            break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Inside the following function, I've used a block. But when I call this function,
I could not figure out how to change the value of results inside the
I try to count the number of children inside a div using jquery $(this)
I have a array with this var_dump value: $query = $wpdb->get_results(SELECT COUNT(*) FROM `$table`
I have an SQL query with a nested join: SELECT rh.host, rh.report, COUNT(results.id), COUNT(results_2.id),
Im trying to JOIN 2 tables and count the results. I have these tables;
I am trying to return a number based on the count of results from
Hi I have the following query : Player.select(Players.*, (SELECT COUNT(*) FROM Results WHERE Results.player_id
I have code like this: <?php if(isset($global_info_results)): ?> <?php echo count($global_info_results) ?> <span>Mali Oglasi:
I want to group results of query by the item id and count number

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.