PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] How can you push every n amount of items into a new array in php

 January 23, 2022     arrays, php     No comments   

Issue

lets say there is an array like this [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]. How can add every 7 values into a new array that looks like this [[0,1,2,3,4,5,6],[7,8,9,10,11,12],[13,14,15,16]


Solution

If the arrays will be equally sized (your example has 7 values in the first array and six in the second - not sure if it's a typo or not) then you can do this:

$array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];

$chunkedArrays = array_chunk($array, 7);

print_r($chunkedArrays);

array_chunk documentation here



Answered By - martincarlin87
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing