This plugin provides the PHP function
c2c_array_partition() 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( "aardvark", "bear", "cat", "dog", "emu", "fox", "gnu", "hippo", "ibis", "jackal" ); print_r( c2c_array_partition( $topics, 4 ) );
Array (  => Array (  => ant  => bear  => cat )  => Array (  => dog  => emu  => fox )  => Array (  => gnu  => hippo )  => Array (  => ibis  => jackal ) )
Note the array elements are distributed into the requested 4 “columns” as evenly as possible.
The function 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.
In contrast, using PHP’s built-in
array_chunk() as such:
print_r( array_chunk( $topics, 4 ) );
Array (  => Array (  => aardvark  => bear  => cat  => dog )  => Array (  => emu  => fox  => gnu  => hippo )  => Array (  => ibis  => jackal ) )
It can be sent an array of any data types or objects.
<?php $topics = array( "ant", "bear", "cat" ); print_r( c2c_array_partition( $topics, 5 ) ); ?>
Array (  => Array (  => ant )  => Array (  => bear )  => Array (  => cat )  => Array ( )  => Array ( ) )
Also see Description section for another example. Definitely check out the packaged unit test file as it contains various examples.
/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
c2c_array_partition()function in your template(s) or code as desired.
- Why not use PHP’s built-in `array_chunk()`?
array_chunk() allows you to specify the number of elements per partition, not how many partitions you want. (See Description.).
For an array with 12 elements, if you requested a chunk size of 2, you would get back an array of 6 sub-arrays (the original elements grouped into arrays of 2 elements). With
array_partition(), if you requested 2 partitions, you would get back an array of 2 sub-arrays (the original elements grouped into 2 arrays).
- Does this plugin include unit tests?
Contributors & Developers
“array_partition” is open source software. The following people have contributed to this plugin.Contributors
- Change: Enable more error output for unit tests.
- Change: Default
/tmp/wordpress-tests-librather than erroring out if not defined via environment variable.
- Change: Minor inline documentation reformatting.
- Change: Note compatibility through WP 4.7+.
- Change: Update copyright date (2017).
- Change: Note compatibility through WP 4.4+.
- Change: Fix minor typo in documentation.
- Change: Explicitly declare methods in unit tests as public or protected.
- Change: Update copyright date (2016).
- Note compatibility through WP 4.1+
- Update copyright date (2015)
- Die early if script is directly invoked
- Minor plugin header reformatting
- Change documentation links to wp.org to be https
- Note compatibility through WP 4.0+
- Add plugin icon
- Return an empty array if 0 columns are requested
- Treat $number_of_columns as absint(), permitting negative and string numerical values to work
- Add unit tests
- Note compatibility through WP 3.8+
- Update copyright date (2014)
- Change description
- Various code and documentation reformatting
- Add banner image
- Shortened description and extended description
- Note compatibility through WP 3.5+
- Update copyright date (2013)
- Re-license as GPLv2 or later (from X11)
- Add ‘License’ and ‘License URI’ header tags to readme.txt and plugin file
- Remove ending PHP close tag
- Minor formatting changes (indentation)
- Note compatibility through WP 3.4+
- Update copyright date (2012)
- Note compatibility through WP 3.3+
- Tweak to plugin description
- Minor additional documentation
- Minor documentation reformatting (spacing)
- Note compatibility through WP 3.2+
- Minor code formatting changes (spacing)
- Fix plugin homepage and author links in description in readme.txt
c2c_array_partition()(but maintain a deprecated version for backwards compatibility)
- Add link to plugin homepage to description in readme.txt
- Note compatibility through WP 3.1+
- Update copyright date (2011)
- Wrap function in if (function_exists()) check
- Note compatibility with WP 3.0+
- Remove docs from top of plugin file (all that and more are in readme.txt)
- Remove trailing whitespace in header docs
- Add Upgrade Notice section to readme.txt
- Add PHPDoc documentation
- Note compatibility with WP 2.9+
- Update copyright date
- Initial release