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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:42:32+00:00 2026-06-13T11:42:32+00:00

Given an array of N items: $arr = array(‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’);

  • 0

Given an array of N items:

$arr = array('a', 'b', 'c', 'd', 'e', 'f');

What’s the most elegant way to loop through in groups of M items (assuming N is divisible by M)?

I tried

foreach (array_chunk($arr, 2) as list($first, $second)) {
    // do stuff with $first and $second
}

but this resulted in a syntax error.

In other words, I want to emulate what in Tcl would look like this:

set arr [a b c d e f]
foreach {first second} $arr {
    // do stuff with $first and $second
}

For now I’ve resorted to the obvious measure:

foreach (array_chunk($arr, 2) as $group) {
    $first  = $group[0];
    $second = $group[1];
    // do stuff with $first and $second
}

But I’m hoping someone has a more elegant method…

  • 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-13T11:42:33+00:00Added an answer on June 13, 2026 at 11:42 am

    I’d alter your last variant to

    foreach (array_chunk($arr, 2) as $group) {
        list( $first, $second) = $group;
        // do stuff with $first and $second
    }
    

    Any other solution I thought about, would be more complex, like

    for ( $i = 0; $i < ceil(count($arr)/2) ; $i++) {
        $first = $arr[$i*2];
        $second = $arr[$i*2+1];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

List item What's the most efficient way to remove items from an array in
Given the following array of json objects: var items = [ {id:1, parentId:0, name:'item1'},
Given an array with n values, for example: $arr[] = 'ABCDEFABC'; $arr[] = 'ABCDEFDEF';
Given the following code: class MagicList def items=(array) @items = array.map{|x| x*2} end def
There are 2 functions involved. Search array items for a given string unset() array
Given an array of items, each of which has a value and cost ,
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
given an array: array = [16 16 16 22 23 23 23 25 52
Given an Array A of n subArrays Sn, how can I select the Array
Given an array of integers A[1...n-1] where N is the length of array A.

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.