- Author:Scott Reilly
- Last update:06 Jan 2010
- Compatibility:All versions of WP
- Comments:go here
- Download:[ zip ]
Introduces function array_partition() to split an array into N number of evenly distributed partitions (useful for splitting data into columns).
Extended DescriptionThis 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”.
$topics = array( "ant", "bear", "cat", "dog", "emu", "frog", "gnu", "hippo", "ibis", "jackal"); print_r( array_partition($topics, 4) );
Array (  => Array (  => ant  => bear  => cat )  => Array (  => dog  => emu  => frog )  => Array (  => gnu  => hippo )  => Array (  => ibis  => 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.
- Download the file array-partition.zip and unzip it into your wp-content/plugins/ directory (or install via the built-in WordPress plugin installer).
- Activate the plugin through the ‘Plugins’ admin menu in WordPress.
- Use the
array_partition()function in your template(s) or code as desired.
$topics = array( "ant", "bear", "cat"); print_r(array_partition($topics, 5));
Array (  => Array (  => ant )  => Array (  => bear )  => Array (  => cat )  => Array ( )  => Array ( ) )
Frequently Asked Questions
Q: Why not use PHP’s built-in
array_chunk()allows you to specify the number of elements per partition, not how many partitions you want.
- 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 & DisclaimerCopyright © 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.
Thanks to all those who have contributed feedback and support!
Code largely taken from: us.php.net/manual/en/function.array-chunk.php#75022