Categories

array_partition

Author:Scott Reilly
Version:1.0.1
Last update:06 Jan 2010
Compatibility:All versions of WP
Comments:go here
Download:[ zip ]
Description:

Introduces function array_partition() to split an array into N number of evenly distributed partitions (useful for splitting data into columns).

Extended Description

This plugin provides a PHP function to split an array into any number of sub-arrays, suitable for creating evenly distributed (vertically-filled) “columns”. Also known as “chunking” or “partitioning”.

For example:


$topics = array( "ant", "bear", "cat", "dog", "emu", "frog", "gnu", "hippo", "ibis", "jackal");
print_r( array_partition($topics, 4) );

Yields:


Array
(
    [0] => Array
        (
            [0] => ant
            [1] => bear
            [2] => cat
        )

    [1] => Array
        (
            [0] => dog
            [1] => emu
            [2] => frog
        )

    [2] => Array
        (
            [0] => gnu
            [1] => hippo
        )

    [3] => Array
        (
            [0] => ibis
            [1] => jackal
        )

)

The plugin will fill as many partitions as requested, as long as there are enough elements in the array to do so. Any remaining unfilled partitions will be represented as empty arrays.

It can be sent an array of any data types or objects.

Installation

  1. Download the file array-partition.zip and unzip it into your wp-content/plugins/ directory (or install via the built-in WordPress plugin installer).
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress.
  3. Use the array_partition() function in your template(s) or code as desired.

Examples

  • 
    $topics = array( "ant", "bear", "cat");
    print_r(array_partition($topics, 5));
    

    =>

    
    Array
    (
        [0] => Array
            (
                [0] => ant
            )
    
        [1] => Array
            (
                [0] => bear
            )
    
        [2] => Array
            (
                [0] => cat
            )
    
        [3] => Array
            (
            )
    
        [4] => Array
            (
            )
    
    )
    

Frequently Asked Questions

  • Q: Why not use PHP’s built-in array_chunk()?

    A: array_chunk() allows you to specify the number of elements per partition, not how many partitions you want.

Release Log

  • 06 Jan 2010 : v1.0.1 — * Add PHPDoc documentation; note compatibility with WP 2.9+; update copyright date
  • 20 Apr 2008 : v1.0 — Release notes.

Copyright & Disclaimer

Copyright © 2008-2010 by Scott Reilly (aka coffee2code)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Acknowledgements

Thanks to all those who have contributed feedback and support!

Code largely taken from: us.php.net/manual/en/function.array-chunk.php#75022

One reply on “array_partition”

Leave a Reply

Your email address will not be published.