Home >Backend Development >PHP Tutorial >[php function]--array function--array_chunk

[php function]--array function--array_chunk

WBOY
WBOYOriginal
2016-07-29 09:01:031230browse

array_chunk

  • Version: >= 4.2
  • Description:
    array_chunk(array $input, int $size, [, bool $preserve_keys=false])
    The function of this function is to divide the array into chunks. This function can be used when we batch process the data in the array.
  • parameter
    • input
      The array to process.
    • size
      Size of array block
    • preserve_keys
      Whether to retain the original data key, the default is not to retain.
  • Example
<code><span>$input</span>=<span>array</span>(<span>"a"</span>=><span>"php"</span>,<span>"b"</span>=><span>"c"</span>,<span>"c"</span>=><span>"c++"</span>,<span>"d"</span>=><span>"python"</span>,<span>"e"</span>=><span>"ruby"</span>);
<span>$output</span> = array_chunk(<span>$input</span>, <span>2</span>);
<span>#output=></span><span>Array</span>
(
    [<span>0</span>] => <span>Array</span>
        (
            [<span>0</span>] => php
            [<span>1</span>] => c
        )

    [<span>1</span>] => <span>Array</span>
        (
            [<span>0</span>] => c++
            [<span>1</span>] => python
        )

    [<span>2</span>] => <span>Array</span>
        (
            [<span>0</span>] => ruby
        )

)

<span>$output2</span> = array_chunk(<span>$input</span>, <span>2</span>, <span>true</span>);
<span>#output2=> </span><span>Array</span>
(
    [<span>0</span>] => <span>Array</span>
        (
            [a] => php
            [b] => c
        )

    [<span>1</span>] => <span>Array</span>
        (
            [c] => c++
            [d] => python
        )

    [<span>2</span>] => <span>Array</span>
        (
            [e] => ruby
        )

)
<span>#注意子数组中的key还是保留原来数组中的key</span></code>
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced [php function]--array function--array_chunk, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn