array_partition

描述

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( "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

Examples

<?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 packaged unit test file as it contains various examples.

安裝

  1. Install via the built-in WordPress plugin installer. Or download and unzip array-partition.zip inside the plugins directory for your site (typically wp-content/plugins/)
  2. Activate the plugin through the ‘Plugins’ admin menu in WordPress
  3. 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).

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.

Does this plugin include unit tests?

Yes.

評價

There are no reviews for this plugin.

貢獻者及開發者

“array_partition” 是一個開源的軟體。以下的人對這個外掛作出了貢獻。

貢獻者

將 array_partition 外掛本地化為台灣繁體中文版。

對開發相關資訊感興趣?

任何人均可瀏覽程式碼、查看 SVN 存放庫,或透過 RSS 訂閱開發記錄

修改日誌

1.3.3 (2023-05-21)

  • Change: Note compatibility through WP 6.3+
  • Change: Update copyright date (2023)

1.3.2 (2021-09-25)

  • Change: Note compatibility through WP 5.8+
  • Unit tests:
    • Change: Restructure unit test directories
      • Change: Move phpunit/ into tests/phpunit/
      • Change: Move phpunit/bin/ into tests/
    • Change: Remove ‘test-‘ prefix from unit test file
    • Change: In bootstrap, store path to plugin file constant
    • Change: In bootstrap, add backcompat for PHPUnit pre-v6.0

1.3.1 (2021-04-05)

  • Change: Note compatibility through WP 5.7+
  • Change: Update copyright date (2021)
  • Fix: Add missing word in an installation instruction

Full changelog is available in CHANGELOG.md.