开发者 | coffee2code |
---|---|
更新时间 | 2024年8月11日 13:30 |
捐献地址: | 去捐款 |
PHP版本: | 1.2 及以上 |
WordPress版本: | 6.6 |
版权: | GPLv2 or later |
版权网址: | 版权信息 |
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 ) );
Yields:
`Array
(
[0] => Array
(
[0] => ant
[1] => bear
[2] => cat
)
[1] => Array
(
[0] => dog
[1] => emu
[2] => fox
)
[2] => Array
(
[0] => gnu
[1] => hippo
)
[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
array-partition.zip
inside the plugins directory for your site (typically wp-content/plugins/
)c2c_array_partition()
function in your template(s) or code as desired.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. With c2c_array_partition()
, you tell it how many partitions you want, and it'll evenly split the elements into those partitions as evenly as possible.
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.
.gitignore
filereadme.txt
bootstrap.php
composer.json
for PHPUnit Polyfill dependencyphpunit/
into tests/phpunit/
phpunit/bin/
into tests/