Jump to content
  • Hello visitors, welcome to the Hacker World Forum!

    Red Team 1949  (formerly CHT Attack and Defense Team) In this rapidly changing Internet era, we maintain our original intention and create the best community to jointly exchange network technologies. You can obtain hacker attack and defense skills and knowledge in the forum, or you can join our Telegram communication group to discuss and communicate in real time. All kinds of advertisements are prohibited in the forum. Please register as a registered user to check our usage and privacy policy. Thank you for your cooperation.

    TheHackerWorld Official

PHP array_chunk()函数

 Share


KaiWn

Recommended Posts

  • array_chunk()函数是PHP中的内置函数,用于根据传递给函数的参数将数组拆分为给定大小的部分或块。最后一个块可能包含的元素少于块的所需大小。

    语法:

    array array_chunk$ array$ size$ preserve_keys

    参数:该函数接受三个参数,如上面的语法所示。参数说明如下:

    • $array:此参数表示需要分成多个块的数组。
    • $size:此参数是整数,用于定义要创建的块的大小。
    • $preserve_keys:此参数为布尔值。当此参数设置为TRUE时,将保留键,否则将从0开始重新索引块。

    返回值:该函数返回一个从0开始索引的多维数组。每个块包含$size个元素,最后一个块除外,后者可能包含较少数量的元素。

    实例:

    Input : $input_array = array('a', 'b', 'c', 'd', 'e');
            array_chunk($input_array, 2);
    Output : Array(
                    [0] => Array
                    (
                        [0] => a
                        [1] => b
                    )
                    [1] => Array
                    (
                        [0] => c
                        [1] => d
                    )
                    [2] => Array
                    (
                        [0] => e
                    )
                )
    
    Input : $input_array = array('a', 'b', 'c', 'd', 'e');
           array_chunk($input_array, 2, true)
    Output :    Array
                (
                    [0] => Array
                    (
                        [0] => a
                        [1] => b
                    )
                    [1] => Array
                    (
                        [2] => c
                        [3] => d
                    )
                    [2] => Array
                    (
                        [4] => e
                    )
                )   

    在第一个示例中,返回一个多维数组,其中每个块均包含2个元素。在第二个示例中,由于第三个参数作为true传递,因此每个块中元素的索引与从中创建该块的原始数组中它们的索引相同。在这种情况下,每个块包含2个元素,它们是传递给该函数的size的值。

    再看两个实例:

    实例一:

    <?php
      
    $input_array = array('a', 'b', 'c', 'd', 'e');
      
    print_r(array_chunk($input_array, 2));
      
    ?>

    输出:

    Array
    (
        [0] => Array
            (
                [0] => a
                [1] => b
            )
        [1] => Array
            (
                [0] => c
                [1] => d
            )
        [2] => Array
            (
                [0] => e
            )
    )

    实例二:

    <?php
      
    $input_array = array('a', 'b', 'c', 'd', 'e');
      
    print_r(array_chunk($input_array, 2, true));
      
    ?>

    输出:

    Array
    (
        [0] => Array
            (
                [0] => a
                [1] => b
            )
        [1] => Array
            (
                [2] => c
                [3] => d
            )
        [2] => Array
            (
                [4] => e
            )
    )

     

    注意事项:

    当使用array_chunk()分割数组,插入数据库时,如果 分割的数组不是等份的时候,则可能导致 数据库自增id 不连续,所以有需求 自增id连续的话,尽量使 array_chunk() 分割的新数组为等份的来避免一些问题。

Link to post
Link to comment
Share on other sites

 Share

discussion group

discussion group

    You don't have permission to chat.
    • Recently Browsing   0 members

      • No registered users viewing this page.
    ×
    ×
    • Create New...