Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP array_chunk function

The PHP array_chunk function is responsible for dividing a provided array into chunks or parts. It will return a new array, which will be the one that is divided into segments as the result.

Description / Descripción
array_chunk(array $array, int $size, bool $preserve_keys = false): array

Parameters

$array – The array you provide for fragmentation.

$size – The size each chunk will have. Keep in mind.

$preserve_keys – This parameter is optional. If you want to preserve the keys of the provided array, you can pass true in this parameter.

Return

An array with subarrays that will be its chunks.

The array_chunk function can be very useful, even if it’s not immediately apparent. Let’s say we have a database query, and we want to paginate the results. It wouldn’t make much sense to constantly query the database every time we need a different page, especially if the site is highly trafficked and has queries happening all over. In such cases, it’s better to retrieve a larger dataset, like 100 records, and then apply array_chunk. This way, we reduce the number of queries. Making thousands of small queries can lead to connection overhead.

PHP array_chunk Examples

A simple example of array_chunck

Este es un ejemplo con marcas de autos
<?php
//blastcoding.com example1 php array_chunk
$marcasDeAutos = array(
    "Toyota",
    "Ford",
    "Honda",
    "Chevrolet",
    "Volkswagen",
    "BMW",
    "Mercedes-Benz",
    "Audi",
    "Nissan",
    "Hyundai",
    "Kia",
    "Subaru",
    "Mazda",
    "Lexus",
    "Jeep",
);

print_r(array_chunk($marcasDeAutos,5));
Array
(
    [0] => Array
        (
            [0] => Toyota
            [1] => Ford
            [2] => Honda
            [3] => Chevrolet
            [4] => Volkswagen
        )

    [1] => Array
        (
            [0] => BMW
            [1] => Mercedes-Benz
            [2] => Audi
            [3] => Nissan
            [4] => Hyundai
        )

    [2] => Array
        (
            [0] => Kia
            [1] => Subaru
            [2] => Mazda
            [3] => Lexus
            [4] => Jeep
        )

)

Applying array_chunk for a future pagination

Este caso es el ejemplo que comentamos
//blastcoding.com example2 php array_chunk
$usuarios = array(
    array(
        'nombre' => 'Juan Pérez',
        'edad' => 25,
        'email' => 'juan@example.com',
    ),
    array(
        'nombre' => 'María Rodríguez',
        'edad' => 30,
        'email' => 'maria@example.com',
    ),
    array(
        'nombre' => 'Carlos Sánchez',
        'edad' => 22,
        'email' => 'carlos@example.com',
    ),
    array(
        'nombre' => 'Luis González',
        'edad' => 28,
        'email' => 'luis@example.com',
    ),
    array(
        'nombre' => 'Ana Martínez',
        'edad' => 35,
        'email' => 'ana@example.com',
    ),
    array(
        'nombre' => 'Laura López',
        'edad' => 29,
        'email' => 'laura@example.com',
    ),
    array(
        'nombre' => 'Pedro Torres',
        'edad' => 27,
        'email' => 'pedro@example.com',
    ),
    array(
        'nombre' => 'Elena Ramírez',
        'edad' => 31,
        'email' => 'elena@example.com',
    ),
    array(
        'nombre' => 'Sofía García',
        'edad' => 26,
        'email' => 'sofia@example.com',
    ),
    array(
        'nombre' => 'Mario Díaz',
        'edad' => 24,
        'email' => 'mario@example.com',
    ),
    array(
        'nombre' => 'Javier Fernández',
        'edad' => 33,
        'email' => 'javier@example.com',
    ),
    array(
        'nombre' => 'Isabel Ortega',
        'edad' => 23,
        'email' => 'isabel@example.com',
    ),
    array(
        'nombre' => 'Rosa Jiménez',
        'edad' => 32,
        'email' => 'rosa@example.com',
    ),
    array(
        'nombre' => 'Diego Silva',
        'edad' => 28,
        'email' => 'diego@example.com',
    ),
    array(
        'nombre' => 'Carolina Castro',
        'edad' => 34,
        'email' => 'carolina@example.com',
    ),
);

print_r(array_chunk($usuarios,5));
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [nombre] => Juan Pérez
                    [edad] => 25
                    [email] => juan@example.com
                )

            [1] => Array
                (
                    [nombre] => María Rodríguez
                    [edad] => 30
                    [email] => maria@example.com
                )

            [2] => Array
                (
                    [nombre] => Carlos Sánchez
                    [edad] => 22
                    [email] => carlos@example.com
                )

            [3] => Array
                (
                    [nombre] => Luis González
                    [edad] => 28
                    [email] => luis@example.com
                )

            [4] => Array
                (
                    [nombre] => Ana Martínez
                    [edad] => 35
                    [email] => ana@example.com
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [nombre] => Laura López
                    [edad] => 29
                    [email] => laura@example.com
                )

            [1] => Array
                (
                    [nombre] => Pedro Torres
                    [edad] => 27
                    [email] => pedro@example.com
                )

            [2] => Array
                (
                    [nombre] => Elena Ramírez
                    [edad] => 31
                    [email] => elena@example.com
                )

            [3] => Array
                (
                    [nombre] => Sofía García
                    [edad] => 26
                    [email] => sofia@example.com
                )

            [4] => Array
                (
                    [nombre] => Mario Díaz
                    [edad] => 24
                    [email] => mario@example.com
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [nombre] => Javier Fernández
                    [edad] => 33
                    [email] => javier@example.com
                )

            [1] => Array
                (
                    [nombre] => Isabel Ortega
                    [edad] => 23
                    [email] => isabel@example.com
                )

            [2] => Array
                (
                    [nombre] => Rosa Jiménez
                    [edad] => 32
                    [email] => rosa@example.com
                )

            [3] => Array
                (
                    [nombre] => Diego Silva
                    [edad] => 28
                    [email] => diego@example.com
                )

            [4] => Array
                (
                    [nombre] => Carolina Castro
                    [edad] => 34
                    [email] => carolina@example.com
                )

        )

)
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category