Laravel collections

méthode crossJoin()

Quand est apparue cette méthode ?

depuis Laravel v5.5

Description


Exemples

Il y a 1 exemple pour cette collection

Exemple #1 : Croisons les données !

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

$cross = $languages->crossJoin($level->unique());
echo "<h1>crossJoin by example</h1>" . PHP_EOL;
var_dump($cross);

Résultat

    

crossJoin by example

object(Illuminate\Support\Collection)#1391 (1) { ["items":protected]=> array(24) { [0]=> array(2) { [0]=> string(3) "php" [1]=> string(6) "expert" } [1]=> array(2) { [0]=> string(3) "php" [1]=> string(6) "normal" } [2]=> array(2) { [0]=> string(3) "php" [1]=> string(6) "newbie" } [3]=> array(2) { [0]=> string(6) "python" [1]=> string(6) "expert" } [4]=> array(2) { [0]=> string(6) "python" [1]=> string(6) "normal" } [5]=> array(2) { [0]=> string(6) "python" [1]=> string(6) "newbie" } [6]=> array(2) { [0]=> string(10) "javascript" [1]=> string(6) "expert" } [7]=> array(2) { [0]=> string(10) "javascript" [1]=> string(6) "normal" } [8]=> array(2) { [0]=> string(10) "javascript" [1]=> string(6) "newbie" } [9]=> array(2) { [0]=> string(2) "go" [1]=> string(6) "expert" } [10]=> array(2) { [0]=> string(2) "go" [1]=> string(6) "normal" } [11]=> array(2) { [0]=> string(2) "go" [1]=> string(6) "newbie" } [12]=> array(2) { [0]=> string(2) "c#" [1]=> string(6) "expert" } [13]=> array(2) { [0]=> string(2) "c#" [1]=> string(6) "normal" } [14]=> array(2) { [0]=> string(2) "c#" [1]=> string(6) "newbie" } [15]=> array(2) { [0]=> string(4) "java" [1]=> string(6) "expert" } [16]=> array(2) { [0]=> string(4) "java" [1]=> string(6) "normal" } [17]=> array(2) { [0]=> string(4) "java" [1]=> string(6) "newbie" } [18]=> array(2) { [0]=> string(5) "cobol" [1]=> string(6) "expert" } [19]=> array(2) { [0]=> string(5) "cobol" [1]=> string(6) "normal" } [20]=> array(2) { [0]=> string(5) "cobol" [1]=> string(6) "newbie" } [21]=> array(2) { [0]=> string(5) "basic" [1]=> string(6) "expert" } [22]=> array(2) { [0]=> string(5) "basic" [1]=> string(6) "normal" } [23]=> array(2) { [0]=> string(5) "basic" [1]=> string(6) "newbie" } } }