note 102553 deleted from function.array-chunk by danbrown

[email protected] Mon, 21 Feb 2011 05:51:42 -0800
Newsgroups php.notes
Message-ID <[email protected]>
Note Submitter: ismailbaskin1 at gmail dot com 

----

<?php
function array_chunk_vertical($data, $columns) {
	$newarray = array();
	$i = 0;
	foreach($data as $index => $row){
		$newarray[$i%$columns][$index] = $row;
		$i++;
	}

        return $newarray ;
}

$fruits = array('apple'=>50,'banana'=>35, 'grape' => 85,'pear'=>45,'apricot'=>28,'peach'=>65);

print_r(array_chunk($fruits,2,true));

/*
Classic chunk function
Array
(
    [0] => Array
        (
            [apple] => 50
            [banana] => 35
        )
 
    [1] => Array
        (
            [grape] => 85
            [pear] => 45
        )
 
    [2] => Array
        (
            [apricot] => 28
            [peach] => 65
        )
 
)
*/

echo "\n\n";

print_r(array_chunk_vertical($fruits,2));

/*

vertical chunk function

Array
(
    [0] => Array
        (
            [apple] => 50
            [grape] => 85
            [apricot] => 28
        )
 
    [1] => Array
        (
            [banana] => 35
            [pear] => 45
            [peach] => 65
        )
 
)

*/
?>