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

  • Home
  • SEARCH
  • 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 5845783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:29:02+00:00 2026-05-22T12:29:02+00:00

I have an object which contains 7 items. $obj.gettype().name Object[] $obj.length 7 I want

  • 0

I have an object which contains 7 items.

$obj.gettype().name
Object[]

$obj.length
7

I want to loop through in batches of 3. I do NOT want to use the modulus function, I actually want to be able to create a new object with just the 3 items from that batch. Pseudo code:

$j=0
$k=1
for($i=0;$i<$obj.length;$i+=3){
    $j=$i+2
    $myTmpObj = $obj[$i-$j] # create tmpObj which contains items 1-3, then items 4-6, then 7 etc
    echo "Batch $k
    foreach($item in $myTmpObj){
        echo $item
    }
    $k++
 }

Batch 1
item 1
item 2
item 3

Batch 2
item 4
item 5
item 6

Batch 3
Item 7

Regards,
ted

  • 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-22T12:29:03+00:00Added an answer on May 22, 2026 at 12:29 pm

    Your pseudo code is almost real. I have just changed its syntax and used the range operator (..):

    # demo input (btw, also uses ..)
    $obj = 1..7
    
    $k = 1
    for($i = 0; $i -lt $obj.Length; $i += 3) {
    
        # end index
        $j = $i + 2
        if ($j -ge $obj.Length) {
            $j = $obj.Length - 1
        }
    
        # create tmpObj which contains items 1-3, then items 4-6, then 7 etc
        $myTmpObj = $obj[$i..$j]
    
        # show batches
        "Batch $k"
        foreach($item in $myTmpObj) {
            $item
        }
        $k++
    }
    

    The output looks exactly as required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a SelectOneMenu driven by a List, each of which contains an object
I have a Tuple<T1, T2> which contains the same object that are in a
I have a complex view model which contains a collection of other objects which
I have an already created DataTable object which i am using for my girdview
The problem is strange, I have a combobox which is correctly filled with items
I have ran into a really annoying issue which gives me this error: The
I have a wcf service created that's being consumed by a silverlight application. We
Inside my view.py, I have two functions, one that processes input from a form
I am making a website that displays a user's chosen youtube videos. A user
I'm developing a game for a course at my school. One of the assignments

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.