PHP: Poor man's alternative to array_chunk 
Useful when having to insert a large set of data in chunks to a databse:
<?php
$numbers=array (
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
);

$output = array();

foreach($numbers as $i=>$number)
{
$output[]= $number;

if(($i+1) % 3 == 0 || $i==count($numbers)-1)//last key
{
var_dump($output);
$output=array();
}
}

The above outputs:
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
}
array(3) {
[0]=>
string(1) "4"
[1]=>
string(1) "5"
[2]=>
string(1) "6"
}
array(3) {
[0]=>
string(1) "7"
[1]=>
string(1) "8"
[2]=>
string(1) "9"
}
array(2) {
[0]=>
string(2) "10"
[1]=>
string(2) "11"
}

http://stackoverflow.com/questions/3549 ... -iteration

Thank you very much for your time !!!!!!

Comments
Comments are not available for this entry.
2024 By Angel Cool