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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:31:32+00:00 2026-06-02T04:31:32+00:00

Trying to group an array depending on whether it’s index is divisible by there.

  • 0

Trying to group an array depending on whether it’s index is divisible by there. I’ve tried various combinations of for loops and foreach loops and if($i % 3 == 0) … But, it’s not outputting the way I need it.

Here is a greatly simplified array (the original data contains a lot of highly sensitive information…)

The original array looks similar to this:

$item[0]['Section 1']['Item 2'] => 1334;
$item[1]['Section 2']['Item 3'] => 15454;
$item[2]['Section 3']['Item 4'] => 1452;
$item[3]['Section 4']['Item 5'] => 1341;
$item[4]['Section 5']['Item 6'] => 1788655;
$item[5]['Section 6']['Item 7'] => 13;
$item[6]['Section 7']['Item 8'] => 142;
$item[7]['Section 8']['Item 9'] => 15678;
$item[8]['Section 9']['Item 10'] => 15542;
$item[9]['Section 10']['Item 11'] => 16578;
$item[10]['Section 11']['Item 12'] => 18452;
$item[11]['Section 12']['Item 13'] => 16565;

I’m trying to group every 3 records, like this:

$newitem[0]['Section 1']['Item 2'] => 1334;
$newitem[0]['Section 2']['Item 3'] => 15454;
$newitem[0]['Section 3']['Item 4'] => 1452;
$newitem[1]['Section 4']['Item 5'] => 1341;
$newitem[1]['Section 5']['Item 6'] => 1788655;
$newitem[1]['Section 6']['Item 7'] => 13;
$newitem[2]['Section 7']['Item 8'] => 142;
$newitem[2]['Section 8']['Item 9'] => 15678;
$newitem[2]['Section 9']['Item 10'] => 15542;
$newitem[3]['Section 10']['Item 11'] => 16578;
$newitem[3]['Section 11']['Item 12'] => 18452;
$newitem[3]['Section 12']['Item 13'] => 16565;
  • 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-02T04:31:33+00:00Added an answer on June 2, 2026 at 4:31 am

    This should do it:

    $newindex = 0;
    $newitem = array();
    for($i = 0; $i < count($item); ++$i)
    {
        $newindex = (int) $i / 3;
        if(!isset($newitem[ $newindex ]))
            $newitem[ $newindex ] = array();
    
        $newitem[ $newindex ] = array_merge($newitem[ $newindex ], $item[ $i ]);
    }
    

    Link to output (dump of $newitem)

    The trick is (int) $i / 3, which takes $i, divides it by 3, then casts to int which truncates any decimal places.

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

Sidebar

Related Questions

My group is trying to build a server using Apache Thrift but we are
I am trying to group row data from a 3-dimensional array by a column
I'm trying to use checkboxes to add users to a specific group but I'm
I am trying to group a numpy array into smaller size by taking average
I have a multidimensional array and am trying to group them according to the
I am trying to unset a group of array keys that have the same
I'm trying to get the index of the trigger element in the array. The
I have an array that looks like the one below. I'm trying to group
Im trying to add elements to a dict list (associative array), but every time
I'm trying to arrange a group of pages in to an array and place

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.