Laravel collections

méthode unique()

Quand est apparue cette méthode ?

depuis Laravel v5.1

Description


Exemples

Il y a 3 exemples pour cette collection

Exemple #1 : Exemple simple

Collection utilisée

    
        // 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


$level = collect([
            "expert",
            "normal",
            "normal",
            "newbie",
            "newbie",
            "normal",
            "newbie",
            "expert"
            ]
        );

print_r($level->unique());

Résultat

    
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => expert
            [1] => normal
            [3] => newbie
        )

)
    

Exemple #2 : Dans le cas de collection imbriquées, il te faudra préciser sur quel champ tu veux appliquer le critère d’unicité.

Collection utilisée

    
        // define nested collection
        $this->nested = collect([
            ['name' => 'Spaghetti à la carbonara', 'level' => 'Moyen', 'price' => 'Economique', 'type' => 'Pâtes'],
            ['name' => 'Loup entier grillé', 'level' => 'Chef', 'price' => 'Cher', 'type' => 'Poisson'],
            ['name' => 'Gnocchi au pesto', 'level' => 'Facile', 'price' => 'Economique', 'type' => 'Pâtes'],
            ['name' => 'Daurade vapeur et ses pommes de terre', 'level' => 'Moyen', 'price' => 'Moyen', 'type' => 'Poisson'],
            ['name' => 'Dame blanche', 'level' => 'Moyen', 'price' => 'Moyen', 'type' => 'Dessert'],
            ['name' => 'Banana Split', 'level' => 'Chef', 'price' => 'Cher', 'type' => 'Dessert'],
            ['name' => 'Coupe Colonel', 'level' => 'Facile', 'price' => 'Economique', 'type' => 'Dessert'],
        ]);

        Illuminate\Support\Collection {#440 ▼
            #items: array:7 [▼
              0 => array:4 [▼
                "name" => "Spaghetti à la carbonara"
                "level" => "Moyen"
                "price" => "Economique"
                "type" => "Pâtes"
              ]
              1 => array:4 [▼
                "name" => "Loup entier grillé"
                "level" => "Chef"
                "price" => "Cher"
                "type" => "Poisson"
              ]
              2 => array:4 [▼
                "name" => "Gnocchi au pesto"
                "level" => "Facile"
                "price" => "Economique"
                "type" => "Pâtes"
              ]
              3 => array:4 [▼
                "name" => "Daurade vapeur et ses pommes de terre"
                "level" => "Moyen"
                "price" => "Moyen"
                "type" => "Poisson"
              ]
              4 => array:4 [▼
                "name" => "Dame blanche"
                "level" => "Moyen"
                "price" => "Moyen"
                "type" => "Dessert"
              ]
              5 => array:4 [▼
                "name" => "Banana Split"
                "level" => "Chef"
                "price" => "Cher"
                "type" => "Dessert"
              ]
              6 => array:4 [▼
                "name" => "Coupe Colonel"
                "level" => "Facile"
                "price" => "Economique"
                "type" => "Dessert"
              ]
            ]
          }
    

Code source


$nested = collect([
            ["name" => "Spaghetti à la carbonara", "level" => "Moyen", "price" => "Economique", "type" => "Pâtes"],
            ["name" => "Loup entier grillé", "level" => "Chef", "price" => "Cher", "type" => "Poisson"],
            ["name" => "Gnocchi au pesto", "level" => "Facile", "price" => "Economique", "type" => "Pâtes"],
            ["name" => "Daurade vapeur et ses pommes de terre", "level" => "Moyen", "price" => "Moyen", "type" => "Poisson"],
            ["name" => "Dame blanche", "level" => "Moyen", "price" => "Moyen", "type" => "Dessert"],
            ["name" => "Banana Split", "level" => "Chef", "price" => "Cher", "type" => "Dessert"],
            ["name" => "Coupe Colonel", "level" => "Facile", "price" => "Economique", "type" => "Dessert"],
        ]);

print_r($nested->unique('level'));

Résultat

    
Illuminate\Support\Collection Object
(
    [items:protected] => Array
        (
            [0] => Array
                (
                    [name] => Spaghetti à la carbonara
                    [level] => Moyen
                    [price] => Economique
                    [type] => Pâtes
                )

            [1] => Array
                (
                    [name] => Loup entier grillé
                    [level] => Chef
                    [price] => Cher
                    [type] => Poisson
                )

            [2] => Array
                (
                    [name] => Gnocchi au pesto
                    [level] => Facile
                    [price] => Economique
                    [type] => Pâtes
                )

        )

)
    

Exemple #3 : Collections imbriquées et fonction de callback

Collection utilisée

    
        // define nested collection
        $this->nested = collect([
            ['name' => 'Spaghetti à la carbonara', 'level' => 'Moyen', 'price' => 'Economique', 'type' => 'Pâtes'],
            ['name' => 'Loup entier grillé', 'level' => 'Chef', 'price' => 'Cher', 'type' => 'Poisson'],
            ['name' => 'Gnocchi au pesto', 'level' => 'Facile', 'price' => 'Economique', 'type' => 'Pâtes'],
            ['name' => 'Daurade vapeur et ses pommes de terre', 'level' => 'Moyen', 'price' => 'Moyen', 'type' => 'Poisson'],
            ['name' => 'Dame blanche', 'level' => 'Moyen', 'price' => 'Moyen', 'type' => 'Dessert'],
            ['name' => 'Banana Split', 'level' => 'Chef', 'price' => 'Cher', 'type' => 'Dessert'],
            ['name' => 'Coupe Colonel', 'level' => 'Facile', 'price' => 'Economique', 'type' => 'Dessert'],
        ]);

        Illuminate\Support\Collection {#440 ▼
            #items: array:7 [▼
              0 => array:4 [▼
                "name" => "Spaghetti à la carbonara"
                "level" => "Moyen"
                "price" => "Economique"
                "type" => "Pâtes"
              ]
              1 => array:4 [▼
                "name" => "Loup entier grillé"
                "level" => "Chef"
                "price" => "Cher"
                "type" => "Poisson"
              ]
              2 => array:4 [▼
                "name" => "Gnocchi au pesto"
                "level" => "Facile"
                "price" => "Economique"
                "type" => "Pâtes"
              ]
              3 => array:4 [▼
                "name" => "Daurade vapeur et ses pommes de terre"
                "level" => "Moyen"
                "price" => "Moyen"
                "type" => "Poisson"
              ]
              4 => array:4 [▼
                "name" => "Dame blanche"
                "level" => "Moyen"
                "price" => "Moyen"
                "type" => "Dessert"
              ]
              5 => array:4 [▼
                "name" => "Banana Split"
                "level" => "Chef"
                "price" => "Cher"
                "type" => "Dessert"
              ]
              6 => array:4 [▼
                "name" => "Coupe Colonel"
                "level" => "Facile"
                "price" => "Economique"
                "type" => "Dessert"
              ]
            ]
          }
    

Code source


$nested = collect([
            ["name" => "Spaghetti à la carbonara", "level" => "Moyen", "price" => "Economique", "type" => "Pâtes"],
            ["name" => "Loup entier grillé", "level" => "Chef", "price" => "Cher", "type" => "Poisson"],
            ["name" => "Gnocchi au pesto", "level" => "Facile", "price" => "Economique", "type" => "Pâtes"],
            ["name" => "Daurade vapeur et ses pommes de terre", "level" => "Moyen", "price" => "Moyen", "type" => "Poisson"],
            ["name" => "Dame blanche", "level" => "Moyen", "price" => "Moyen", "type" => "Dessert"],
            ["name" => "Banana Split", "level" => "Chef", "price" => "Cher", "type" => "Dessert"],
            ["name" => "Coupe Colonel", "level" => "Facile", "price" => "Economique", "type" => "Dessert"],
        ]);

var_dump($nested->unique(function ($item) {
    return $item['level'].$item['price'];
}));

Résultat

    
object(Illuminate\Support\Collection)#1421 (1) {
  ["items":protected]=>
  array(4) {
    [0]=>
    array(4) {
      ["name"]=>
      string(25) "Spaghetti à la carbonara"
      ["level"]=>
      string(5) "Moyen"
      ["price"]=>
      string(10) "Economique"
      ["type"]=>
      string(6) "Pâtes"
    }
    [1]=>
    array(4) {
      ["name"]=>
      string(19) "Loup entier grillé"
      ["level"]=>
      string(4) "Chef"
      ["price"]=>
      string(4) "Cher"
      ["type"]=>
      string(7) "Poisson"
    }
    [2]=>
    array(4) {
      ["name"]=>
      string(16) "Gnocchi au pesto"
      ["level"]=>
      string(6) "Facile"
      ["price"]=>
      string(10) "Economique"
      ["type"]=>
      string(6) "Pâtes"
    }
    [3]=>
    array(4) {
      ["name"]=>
      string(37) "Daurade vapeur et ses pommes de terre"
      ["level"]=>
      string(5) "Moyen"
      ["price"]=>
      string(5) "Moyen"
      ["type"]=>
      string(7) "Poisson"
    }
  }
}