Description
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”.
For example:
$topics = array( "ant", "bear", "cat", "dog", "emu", "fox", "gnu", "hog", "ibis", "jackal" );
print_r( c2c_array_partition( $topics, 4 ) );
Yields:
Array
(
[0] => Array
(
[0] => ant
[1] => bear
[2] => cat
)
[1] => Array
(
[0] => dog
[1] => emu
[2] => fox
)
[2] => Array
(
[0] => gnu
[1] => hog
)
[3] => Array
(
[0] => ibis
[1] => 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 ) );
Yields:
Array
(
[0] => Array
(
[0] => aardvark
[1] => bear
[2] => cat
[3] => dog
)
[1] => Array
(
[0] => emu
[1] => fox
[2] => gnu
[3] => hippo
)
[2] => Array
(
[0] => ibis
[1] => jackal
)
)
It can be sent an array of any data types or objects.
Links: Plugin Homepage | Plugin Directory Page | GitHub | Author Homepage
Nümunələr
<?php
$topics = array( "ant", "bear", "cat" );
print_r( c2c_array_partition( $topics, 5 ) );
?>
=>
Array
(
[0] => Array
(
[0] => ant
)
[1] => Array
(
[0] => bear
)
[2] => Array
(
[0] => cat
)
[3] => Array
(
)
[4] => Array
(
)
)
Also see Description section for another example. Definitely check out the unit test file in the GitHub repository as it contains various examples (see FAQ).
Installation
- Install via the built-in WordPress plugin installer. Or download and unzip
array-partition.zipinside the plugins directory for your site (typicallywp-content/plugins/) - Activate the plugin through the ‘Plugins’ admin menu in WordPress
- Use the
c2c_array_partition()function in your WordPress template(s) or code as desired. - Optional. For general PHP usage (outside of WordPress), include the
includes/array_partition.phpfile and callc2c_Array_Partition::partition()instead ofc2c_array_partition(). See FAQ for example.
FAQ
-
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).Phrased another way, with
array_chunk()you tell it how many elements max should be in a partition and it gives you as many partitions as necessary. Withc2c_array_partition(), you tell it how many partitions you want, and it’ll evenly split the elements into those partitions as evenly as possible. -
Can I use this function in PHP in a context outside of WordPress?
-
Yes. You just need to include a file from this plugin within your code, and use
c2c_Array_Partition::partition()instead ofc2c_array_partition().require_once 'vendor/array-partition/includes/array_partition.php'; $items = []; // Create an array of items to partition. $partitioned = c2c_Array_Partition::partition( $items, 4 ); -
Does this plugin have unit tests?
-
Yes. The tests are not packaged in the release .zip file or included in plugins.svn.wordpress.org, but can be found in the plugin’s GitHub repository.
Reviews
There are no reviews for this plugin.
Contributors & Developers
“Array Partition” is open source software. The following people have contributed to this plugin.
ContributorsTranslate “Array Partition” into your language.
Interested in development?
Browse the code, check out the SVN repository, or subscribe to the development log by RSS.
Changelog
2.0 (2026-08-22)
Highlights:
This release is largely a housekeeping release that updates the plugin’s name, splits the code into a plugin loader and library file (making non-WP PHP usage more straightfoward), limits support to WP 5.0-7.1+ and PHP 7.0-8.5+, fixes some documentation, updates and expands unit testing, and updates the copyright date (2026).
Details:
- Change: Split code into a plugin loader and library file
- New: Library invocation of function is now
c2c_Array_Partition::partition(...)
- New: Library invocation of function is now
- New: Add type hints to function signature
- Change: Rename to “Array Partition”
- Change: Only accept positive integer values for number of columns
- Change: Switch to using short array syntax
- Change: Use
intdiv()instead offloor()for partition size calculation - Change: Note compatibility through WP 7.1+
- Change: Drop compatibility with versions of WP older than 5.0
- Change: Note compatibility through PHP 8.5+
- Change: Drop compatibility with versions of PHP older than 7.0
- Change: Update copyright date (2026)
- Change: Remove the ‘Text Domain’ plugin header
- Fix: Minor documentation fixes
1.3.5 (2025-04-16)
- Change: Use correct value for ‘Text Domain’ in plugin header
- Change: Note compatibility through WP 6.8+
- Change: Note compatibility through PHP 8.3+
- Change: Update copyright date (2025)
1.3.4 (2024-08-02)
- Change: Note compatibility through WP 6.6+
- Change: Update copyright date (2024)
- New: Add
.gitignorefile - Change: Reduce number of ‘Tags’ from
readme.txt - Change: Remove development and testing-related files from release packaging
- Unit tests:
- Hardening: Prevent direct web access to
bootstrap.php - Allow tests to run against current versions of WordPress
- New: Add
composer.jsonfor PHPUnit Polyfill dependency - Change: In bootstrap, store path to plugin directory in a constant
- Hardening: Prevent direct web access to
Full changelog is available in CHANGELOG.md.
