Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,011 questions

51,958 answers

573 users

How to split an array into chunks in PHP

2 Answers

0 votes
$arr = array("a", "new", "advanced", "operating", "system", "for", "PC");
  
$chunks = array_chunk($arr, 2);

print_r($chunks);



/*
run:
 
Array
(
    [0] => Array
        (
            [0] => a
            [1] => new
        )

    [1] => Array
        (
            [0] => advanced
            [1] => operating
        )

    [2] => Array
        (
            [0] => system
            [1] => for
        )

    [3] => Array
        (
            [0] => PC
        )

)
 
*/

 



answered Jul 4, 2015 by avibootz
edited Nov 26, 2021 by avibootz
0 votes
$arr = array("a", "new", "advanced", "operating", "system", "for", "PC", "MAC", "and", "Linux");
  
$chunks = array_chunk($arr, 3);
 
for ($row = 0; $row < count($chunks); $row++) {
  echo "\n--- Chunk : $row" . "\n";
  for ($col = 0; $col < count($chunks[$row]); $col++) 
        echo $chunks[$row][$col] . "\n";
}



 
/*
run:
 
--- Chunk : 0
a
new
advanced

--- Chunk : 1
operating
system
for

--- Chunk : 2
PC
MAC
and

--- Chunk : 3
Linux
 
*/

 



answered Jul 4, 2015 by avibootz
edited Nov 26, 2021 by avibootz

Related questions

1 answer 108 views
1 answer 124 views
1 answer 136 views
4 answers 262 views
1 answer 102 views
2 answers 123 views
...