array_chunk

Views: 5 Last modified: August 04th, 2011 Comments: 0

The built-in function array_chunk() splits an array into chunks. This function accepts 2 parameters. The first one would be the array, and the second one would be the amount. This function returns a new multidimensional array.

<?php

// Fill array
$arr = array('PHP', 'HTML', 'CSS', 'XHTML', 'SGML', 'JSP');

// Chunk into X pieces
$chuncked = array_chunk($arr, 2);

// Show the result
echo '<pre>'. print_r($chuncked, true) .'</pre>';

?>

The above example would result in:

Array
(
    [0] => Array
        (
            [0] => PHP
            [1] => HTML
        )

    [1] => Array
        (
            [0] => CSS
            [1] => XHTML
        )

    [2] => Array
        (
            [0] => SGML
            [1] => JSP
        )

)
VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)
    Bluehost

    Mail this!

    To: From:Sum {4+9} =  
    Anything to add ?

        You must be logged in to post a comment.