Laravel collections

méthode collapse()

Quand est apparue cette méthode ?

depuis Laravel < v5.1

Description


Exemples

Il y a 1 exemple pour cette collection

Exemple #1 : On 'chunk' et on 'collapse' la collection languages :D

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"
            ]);

$chunked_languages = $languages->chunk(3);

echo "<h2>CHUNKED</h2>" . PHP_EOL;
print_r($chunked_languages);

$collapse_languages = $chunked_languages->collapse();

echo "<h2>COLLAPSED</h2>" . PHP_EOL;
print_r($collapse_languages);

Résultat

    

CHUNKED

Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => php [1] => python [2] => javascript ) ) [1] => Illuminate\Support\Collection Object ( [items:protected] => Array ( [3] => go [4] => c# [5] => java ) ) [2] => Illuminate\Support\Collection Object ( [items:protected] => Array ( [6] => cobol [7] => basic ) ) ) )

COLLAPSED

Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => php [1] => python [2] => javascript [3] => go [4] => c# [5] => java [6] => cobol [7] => basic ) )