Laravel collections

méthode zip()

Quand est apparue cette méthode ?

depuis Laravel... 4, 5, 6 ou 7 :D

Description


Exemples

Il y a 1 exemple pour cette collection

Exemple #1 : Languages level

Collection utilisée

    
        // define languages collection
        $this->languages = collect([
            'php',
            'python',
            'javascript',
            'go',
            'c#',
            'java',
            'cobol',
            'basic'
        ]);

        Illuminate\Support\Collection {#413 ▼
            #items: array:8 [▼
                0 => "php"
                1 => "python"
                2 => "javascript"
                3 => "go"
                4 => "c#"
                5 => "java"
                6 => "cobol"
                7 => "basic"
            ]
        }
    

Code source


$languages = collect([
            "php",
            "python",
            "javascript",
            "go",
            "c#",
            "java",
            "cobol",
            "basic"
            ]);

$zipped = $languages->zip(['5/5', '2/5', '3/5', '0/5', '1/5', '1/5', null, '3/5']);

$zipped->all();

print_r($zipped);

Résultat

    
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => php
                            [1] => 5/5
                        )

                )

            [1] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => python
                            [1] => 2/5
                        )

                )

            [2] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => javascript
                            [1] => 3/5
                        )

                )

            [3] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => go
                            [1] => 0/5
                        )

                )

            [4] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => c#
                            [1] => 1/5
                        )

                )

            [5] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => java
                            [1] => 1/5
                        )

                )

            [6] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => cobol
                            [1] => 
                        )

                )

            [7] => Illuminate\Support\Collection Object
                (
                    [items:protected] => Array
                        (
                            [0] => basic
                            [1] => 3/5
                        )

                )

        )

)