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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:34:55+00:00 2026-06-17T17:34:55+00:00

I have an array of divs (blog posts). Say 33 blog posts. My task

  • 0

I have an array of divs (blog posts). Say 33 blog posts. My task is to split them first into pairs (wrap every other two divs by another) so that I had 17 divs (each containing a pair of blog posts, the last one may contain only one blog post) and then – I want these 15 pair-divs to be split again into groups of 5 (the last one again may contain fewer)… so that in the end I had 3 blocks each containing maximum of 5 blocks each containing two blog posts. Do I make myself clear enough?

Of course the total number of all the posts is unknown so I have to make it work automagically.

Searching for an answer I found out about the function named array_chunk which does exactly that – splits an array into arrays… in my case – I have to run this function twice. And it works perfectly 🙂

$posts = array( 1,2,3,4 ... 33 );
$pairs = array_chunk( $posts, 2 ) // $pairs will contain an array of 17 arrays each containing two elements
$wrap = array_chunk( $pairs, 5 ) // $wrap will contain an array of 3 arrays each containing an array of pairs

The only thing that bothers me is that when I output all the divs via foreach I happen to naturally have 3 nested foreaches… first to render 3 (or more) big wrappers – then – 5 pair wrappers and finally – a foreach to render two blog posts.

I am aware of another solution – to run a foreach once on an original array and arange some mathematical conditions (e.g. if( $i % 2 == 0 ) { //do stuff }) to open and close suitable divs at appropriate moments. Which I haven’t been able to accomplish and would be really glad if someone could help me out with this one if this method is the one to follow.

My questions are as follows:

  • Is there a significant difference (in terms of performance) as to how
    many nested foreaches to run?
  • Is there some kind of a best practice as to which method to use in
    such situations? Or probably there is a totally different approach.
  • 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-17T17:34:56+00:00Added an answer on June 17, 2026 at 5:34 pm

    Is there a significant difference (in terms of performance) as to how many nested foreaches to run?

    Lets say you have 100 posts. So it’s 50 pairs, 10 wraps.

    • Nested foreach: 50 loops * 10 loops = 100 loops
    • Single big loop using %: 100 loops…

    Same thing in both cases. And array_chunk usage will not cause any major difference in perfomance.

    • Benchmark: http://codepad.org/KtEchEmR (I believe comparision of byte is faster than interger)

    Is there some kind of a best practice as to which method to use in such situations? Or probably there is a totally different approach.

    No. But I would rather go for nested loops as it will looks cleaner for another programmer, and you are able to wrap a function around every sub-loop if you need

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

Sidebar

Related Questions

I want to insert all divs which have the class .page into an array,
Say we have array of arrays: tree_limbs = Array.new tree_limbs << %w(1 2 3
Let's say .. I have the following script in my controller: $result= array(Name=>Charlie Sheen,
I have an array of floating divs that fit 4 to a row. Their
I have an observable array, bound to the list of contenteditable divs. I have
I have an observable array that contains a nested tree array. Every array item
I have severals divs and an array. I insert each of the divs (id)
I have 2 divs that I am creating from an array: $.each(data, function(i,item) {
I want to have 10 divs that when someone click on each of them
Let's say I have an array of jQuery objects and wish to have one

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.