Laravel collections

méthode concat()

Quand est apparue cette méthode ?

depuis Laravel v5.4

Description


Exemples

Il y a 1 exemple pour cette collection

Exemple #1 : Combiner plusieurs collections en une seule

Collections utilisées

    
        // define complexe collection
        $this->complexe = collect(
            [
                ['name' => 'php',
                'python',
                'javascript',
                'go',
                'c#',
                'java',
                'cobol',
                'basic'],
                [-2, 200.3, -7.8, 400.1],
                ['ref' => 'XZ42', 'price' => 200.7, 'tags' => ['red', 'new']],
                'totalprice' => 422
            ]
        );

        Illuminate\Support\Collection {#423 ▼
            #items: array:4 [▼
              0 => array:8 [▼
                "name" => "php"
                0 => "python"
                1 => "javascript"
                2 => "go"
                3 => "c#"
                4 => "java"
                5 => "cobol"
                6 => "basic"
              ]
              1 => array:4 [▼
                0 => -2
                1 => 200.3
                2 => -7.8
                3 => 400.1
              ]
              2 => array:3 [▼
                "ref" => "XZ42"
                "price" => 200.7
                "tags" => array:2 [▼
                  0 => "red"
                  1 => "new"
                ]
              ]
              "totalprice" => 422
            ]
          }
    
    
        // 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"
            ]
        }
    
    
        // define Level collection
        $this->level = collect([
            'expert',
            'normal',
            'normal',
            'newbie',
            'newbie',
            'normal',
            'newbie',
            'expert'
            ]
        );

        Illuminate\Support\Collection {#422 ▼
            #items: array:8 [▼
              0 => "expert"
              1 => "normal"
              2 => "normal"
              3 => "newbie"
              4 => "newbie"
              5 => "normal"
              6 => "newbie"
              7 => "expert"
            ]
          }
    
                
                    // define collection numbers
                    $numbers = collect([-2, 200.3, -7.8, 400.1]);

                    Illuminate\Support\Collection {#416 ▼
                        #items: array:4 [▼
                          0 => -2
                          1 => 200.3
                          2 => -7.8
                          3 => 400.1
                        ]
                      }
                
            

Code source


$complexe = collect(
            [
                ["name" => "php",
                "python",
                "javascript",
                "go",
                "c#",
                "java",
                "cobol",
                "basic"],
                [-2, 200.3, -7.8, 400.1],
                ["ref" => "XZ42", "price" => 200.7, "tags" => ["red", "new"]],
                "totalprice" => 422
            ]
        );
$languages = collect([
            "php",
            "python",
            "javascript",
            "go",
            "c#",
            "java",
            "cobol",
            "basic"
            ]);
$level = collect([
            "expert",
            "normal",
            "normal",
            "newbie",
            "newbie",
            "normal",
            "newbie",
            "expert"
            ]
        );
$numbers = collect([-2, 200.3, -7.8, 400.1]);

$concat = $languages->concat($level)->concat($numbers)->concat($complexe);
print_r($concat);

Résultat

    
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => php
            [1] => python
            [2] => javascript
            [3] => go
            [4] => c#
            [5] => java
            [6] => cobol
            [7] => basic
            [8] => expert
            [9] => normal
            [10] => normal
            [11] => newbie
            [12] => newbie
            [13] => normal
            [14] => newbie
            [15] => expert
            [16] => -2
            [17] => 200.3
            [18] => -7.8
            [19] => 400.1
            [20] => Array
                (
                    [name] => php
                    [0] => python
                    [1] => javascript
                    [2] => go
                    [3] => c#
                    [4] => java
                    [5] => cobol
                    [6] => basic
                )

            [21] => Array
                (
                    [0] => -2
                    [1] => 200.3
                    [2] => -7.8
                    [3] => 400.1
                )

            [22] => Array
                (
                    [ref] => XZ42
                    [price] => 200.7
                    [tags] => Array
                        (
                            [0] => red
                            [1] => new
                        )

                )

            [23] => 422
        )

    [escapeWhenCastingToString:protected] => 
)