Laravel collections

méthode combine()

Quand est apparue cette méthode ?

depuis Laravel v5.2

Description


Exemples

Il y a 1 exemple pour cette collection

Exemple #1 : Combinaison de deux collections entre elles

Collections utilisées

    
        // 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"
            ]
          }
    

Code source


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

$result = $languages->combine($level);
print_r($result);

Résultat

    
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [php] => expert
            [python] => normal
            [javascript] => normal
            [go] => newbie
            [c#] => newbie
            [java] => normal
            [cobol] => newbie
            [basic] => expert
        )

)