Laravel系列:类固醇上PHP数组

Laravel collections are one of the most powerful provisions of the Laravel framework. They are what PHP arrays should be, but better.

Laravel集合是Laravel框架最强大的功能之一。 它们就是PHP数组应该是的,但是更好。

Laravel collections are what PHP arrays should be, but better.

Laravel集合应该是PHP数组,但是更好。

In this tutorial, we will be going through a few tricks that may be handy when you are working with collections.

在本教程中,我们将介绍一些技巧,这些技巧在您使用集合时可能会很方便。

收集类 ( The Collection Class )

The Illuminate\Support\Collection class provides a convenient wrapper for working with arrays.

Illuminate\Support\Collection类为使用数组提供了方便的包装器。

The Collection class implements some PHP and Laravel interfaces such as :-

Collection类实现了一些PHP和Laravel接口,例如:

You can check out the rest of the implemented interfaces here.

您可以在此处查看其余已实现的接口。

创建一个新集合 ( Creating A New Collection )

A collection can be created from an array using the collect() helper method or by instantiating the Illuminate\Support\Collection class.

可以使用collect()帮助方法或实例化Illuminate\Support\Collection类从数组创建Illuminate\Support\Collection

A really simple example using the collect() helper method:

一个使用collect()帮助方法的非常简单的示例:

$newCollection = collect([1, 2, 3, 4, 5]);

And a more hashed out one:

还有一个哈希值:

<?php

namespace app\Http\Controllers;

use Illuminate\Support\Collection;

class TestController extends Controller
{
    /**
     * Create a new collection using the collect helper method.
     */
    public function helperCollection()
    {
        $newCollection = collect([1, 2, 3, 4, 5]);
        dd($newCollection);
    }

    /**
     * Create a new collection with a Collection class instance.
     */
    public function classCollection()
    {
        $newCollection = new Collection([1, 2, 3, 4, 5]);
        dd($newCollection);
    }
}

The helper method is much easier easier to work with since you do not need to instantiate the Illuminate\Support\Collection class.

因为不需要实例化Illuminate\Support\Collection类,所以helper方法更容易使用。

I also used the dd() helper method to display the collection on the browser. This should look a little like this.

我还使用了dd()帮助程序方法在浏览器上显示该集合。 这看起来应该像这样。

Sample Collection

雄辩的ORM集合 ( Eloquent ORM Collections )

The Laravel Eloquent ORM also returns data as collections.

Laravel Eloquent ORM还返回数据作为集合。

Eloquent ORM calls return data as collections

雄辩的ORM调用将数据作为集合返回

To demonstrate such feedback, I will set up an sqlite database.

为了展示这种反馈,我将建立一个sqlite数据库。

We will then create a users table with the default migrations that come with Laravel and seed 10 records into the users table.

然后,我们将使用Laravel附带的默认迁移创建一个users表,并将10条记录填充到users表中。

/**
     * Get a list of users from the users table
     */
    public function getUsers()
    {
        $users = User::all();
        dd($users);
    }

The controller method below returns a laravel collection with a list of all users as shown below.

下面的controller方法返回一个laravel集合,其中包含所有用户的列表,如下所示。

List of all users as a collection

You can then simply access a collecton attribute using the arrow notation. For instance, to get the first user's name from the $users collection, we can have the following.

然后,您可以使用箭头表示法简单地访问collecton属性。 例如,要从$users集合中获取第一个用户名,我们可以得到以下内容。

/**
     * Get the name of the first user
     */
    public function firstUser()
    {
        $user = User::first();
        dd($user->name);
    }

创建样本集 ( Creating Our Sample Collection )

We will go through some of the most useful collections tricks that you may find handy.

我们将介绍一些您可能会发现有用的最有用的收藏技巧。

For the next few sections, I will use the following set of data from the users table and some custom collections for demonstration purposes. While we're creating information here manually, we could also use Laravel's model factory

在接下来的几节中,我将使用users表中的以下数据集和一些自定义集合进行演示。 当我们在此处手动创建信息时,我们也可以使用Laravel的模型工厂

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Chasity Tillman
            [email] => qleuschke@example.org
            [age] => 51
            [created_at] => 2016-06-07 15:50:50
            [updated_at] => 2016-06-07 15:50:50
        )
    ...
)

查找数据 ( Finding Data )

There are a number of ways to find data in a collection.

有多种方法可在集合中查找数据。

contains (contains)

The contains() method takes a single value, a key-value pair of parameters or a callback function and returns a boolean value of the value is present in the collection or not.

contains()方法采用单个值,一对键值对参数或一个回调函数,并返回该集合中是否存在该值的布尔值。

/**
     * Check if a collection contains a given key value pair
     * value or callback parameter
     *
     * @return true or false
     */
    public function contains()
    {
        $users = User::all();
        $users->contains('name', 'Chasity Tillman');
        //true

        $collection = collect(['name' => 'John', 'age' => 23]);
        $collection->contains('Jane');
        //false

        $collection = collect([1, 2, 3, 4, 5]);
        $collection->contains(function ($key, $value) {
            return $value <= 5;
            //true
        });
    }

where (where)

You can use the where method to search a collection by a given key, pair value.

您可以使用where方法通过给定的键对值搜索集合。

You can also chain your where() methods . How cool is that?

您也可以链接where()方法。 多么酷啊?

/**
     * Use the where method to find data that matches a given
     * criteria.
     *
     * Chain the methods for fine-tuned criteria
     */
    public function where()
    {
        $users = User::all();
        $user = $users->where('id', 2);
        //Collection of user with an ID of 2

        $user = $users->where('id', 1)
                      ->where('age', '51')
                      ->where('name', 'Chasity Tillman');

        //collection of user with an id of 1, age 51
        //and named Chasity Tillman
    }

There are a few more where-like methods that I will not delve into but you can check them out on the Laravel documentation.

我不会深入探讨一些类似where的方法,但是您可以在Laravel文档中查看它们。

Most notably, you can take a look at:

最值得注意的是,您可以看一下:

  • whereIn() - Takes a key value pair to search but accepts an array of values to search.

    whereIn() -使用键值对进行搜索,但接受值数组进行搜索。

  • search() - Searches for a value in a collection, returns the index if the value is present, and false if it is not.

    search() -在集合中搜索一个值,如果该值存在,则返回索引,否则返回false

  • has() - returns a boolean value if a key value is present in a collection or not.

    has() -如果集合中不存在键值,则返回一个布尔值。

筛选资料 ( Filtering Data )

You have probably guessed it by now that filtering in collections is done using a filter() method.

您现在可能已经猜到了,集合中的过滤是使用filter()方法完成的。

You have also figured it out on your own that the filter method takes a callback function that subjects our collection to a filter test, right? Right?

您还自己弄清楚了filter方法采用了一个回调函数,该函数对我们的集合进行了过滤器测试,对吗? 对?

/**
     * Use the filter method to get a list of all the users that
     * are below the age of 35.
     */
    public function filter()
    {
        $users = User::all();
        $youngsters = $users->filter(function ($value, $key) {
            return $value->age < 35;
        });

        $youngsters->all();
        //list of all users that are below the age of 35
    }

The filter method takes a key and a value as parameters in the callback function. If your condition tests true, the value is added into the assigned retained.

filter方法在回调函数中将键和值作为参数。 如果您的条件测试为true,则将值添加到分配的保留项中。

I have also introduced an all() method which returns the underlying array from a collection. Awesome, right? Right?

我还介绍了all()方法,该方法从集合中返回基础数组。 太好了吧? 对?

排序/订购数据 ( Sorting / Ordering Data )

Collections enable us to sort data using two simple methods :-

集合使我们能够使用两种简单的方法对数据进行排序:

  • sortBy() - Sort data in ascending order given a criteria

    sortBy() -给定条件按升序对数据进行排序
  • sortyByDesc() - Sort data in descending order given a criteria

    sortyByDesc() -给定条件按降序对数据进行排序

The sort methods takes a key or callback function parameter which is used to sort a collection.

sort方法采用用于对集合进行排序的key或callback函数参数。

/**
     * The sort methods takes a key or callback function parameter
     * which is used to sort a collection.
     */
    public function sortData()
    {
        $users  = User::all();

        $youngestToOldest = $users->sortBy('age');
        $youngestToOldest->all();
        //list of all users from youngest to oldest

        $movies = collect([
            [
                'name' => 'Back To The Future',
                'releases' => [1985, 1989, 1990]
            ],
            [
                'name' => 'Fast and Furious',
                'releases' => [2001, 2003, 2006, 2009, 2011, 2013, 2015, 2017]
            ],
            [
                'name' => 'Speed',
                'releases' => [1994]
            ]
        ]);

        $mostReleases = $movies->sortByDesc(function ($movie, $key) {
            return count($movie['releases']);
        });

        $mostReleases->toArray();
        //list of movies in descending order of most releases.

        dd($mostReleases->values()->toArray());
        /*
            list of movies in descending order of most releases
            but with the key values reset
        */
    }

The sort methods maintain the keys for each value. While this may be important for your application, you can reset them to the default zero based incremental values by chaining the values() method.

排序方法维护每个值的键。 尽管这对于您的应用程序可能很重要,但是您可以通过链接values()方法将它们重置为基于零的默认增量值。

As usual, I have also thrown in a new collection method toArray() which simply converts a collection to an array.

和往常一样,我还抛出了一个新的集合方法toArray() ,该方法将集合简单地转换为数组。

分组数据 ( Grouping Data )

###groupBy

### groupBy

Grouping a collection helps makes sense of your data. The groupBy method takes either a key or callback function and returns a grouped collection based on on the key value or the returned callback value.

对集合进行分组有助于理解您的数据。 groupBy方法采用键或回调函数,并根据键值或返回的回调值返回分组的集合。

/**
     * groupBy returns data grouped based on a key or callback function
     * logic
     */
    public function grouping()
    {
        $movies = collect([
            ['name' => 'Back To the Future', 'genre' => 'scifi', 'rating' => 8],
            ['name' => 'The Matrix',  'genre' => 'fantasy', 'rating' => 9],
            ['name' => 'The Croods',  'genre' => 'animation', 'rating' => 8],
            ['name' => 'Zootopia',  'genre' => 'animation', 'rating' => 4],
            ['name' => 'The Jungle Book',  'genre' => 'fantasy', 'rating' => 5],
        ]);

        $genre = $movies->groupBy('genre');
        /*
        [
             "scifi" => [
               ["name" => "Back To the Future", "genre" => "scifi", "rating" => 8,],
             ],
             "fantasy" => [
               ["name" => "The Matrix", "genre" => "fantasy", "rating" => 9,],
               ["name" => "The Jungle Book", "genre" => "fantasy", "rating" => 5, ],
             ],
             "animation" => [
               ["name" => "The Croods", "genre" => "animation", "rating" => 8,],
               ["name" => "Zootopia", "genre" => "animation", "rating" => 4, ],
             ],
        ]
        */

        $rating = $movies->groupBy(function ($movie, $key) {
            return $movie['rating'];
        });

        /*
        [
           8 => [
             ["name" => "Back To the Future", "genre" => "scifi", "rating" => 8,],
             ["name" => "The Croods", "genre" => "animation", "rating" => 8,],
           ],
           9 => [
             ["name" => "The Matrix", "genre" => "fantasy", "rating" => 9,],
           ],
           4 => [
             ["name" => "Zootopia","genre" => "animation", "rating" => 4,],
           ],
           5 => [
             ["name" => "The Jungle Book","genre" => "fantasy","rating" => 5,],
           ],
        ]
       */
    }

获取数据子集 ( Getting A Subset Of Data )

Given an array of data, and subsequently a collection, you may want to get a section of the it. This could be:

给定一个数据数组,然后是一个集合,您可能需要获取其中的一部分。 可能是:

  • The first 2 records

    前2条记录
  • The last 2 records

    最近2条记录
  • All the records but in groups of 2.

    所有记录,但以2组为一组。

Collections bless us with a few methods that do just that.

集合通过一些方法可以使我们受益。

take (take)

The take method takes an integer value and returns the specified number of items. Given a negative number, take() returns the specified number of items from the end of the collection.

take方法采用整数值并返回指定数量的项目。 给定一个负数, take()从集合末尾返回指定数量的项目。

/**
     * The take method returns n number of items in a collection.
     * Given -n, it returns the last n items
     */
    public function takeMe()
    {
        $list = collect([
            'Albert', 'Ben', 'Charles', 'Dan', 'Eric', 'Xavier', 'Yuri', 'Zane'
        ]);

        //Get the first two names
        $firstTwo = $list->take(2);
        //['Albert', 'Ben']

        //Get the last two names
        $lastTwo = $list->take(-2);
        //['Yuri', 'Zane']
    }

chunk (chunk)

The chunk method breaks a collection into smaller collections of the size n.

块方法将一个集合分解为大小为n的较小集合。

/**
     * Chunk(n) returns smaller collections of sizes n each from the
     * original collection.
     */
    public function chunkMe()
    {
        $list = collect([
            'Albert', 'Ben', 'Charles', 'Dan', 'Eric', 'Xavier', 'Yuri', 'Zane'
        ]);

        $chunks = $list->chunk(3);
        $chunks->toArray();
        /*
        [
            ["Albert", "Ben", "Charles",],
            [3 => "Dan", 4 => "Eric", 5 => "Xavier",],
            [6 => "Yuri", 7 => "Zane",],
        ]
        */
    }

There are quite a number of ways in which this may come in handy.

有很多方法可以派上用场。

When you pass the data to a blade view, you can chunk it to get n rows at a time, say, to fit every 3 names into row.

将数据传递到刀片视图时,可以将其分块以一次获得n行,例如,使每3个名称适合一行。

@foreach($list->chunk(3) as $names)<div class="row">
        @foreach($names as $name)
            {{ $name }}
        @endforeach
    </div>
@endforeach

You can also do the reverse of chunk() by combining smaller collections into a larger one using the collapse() method. Check it out here.

您也可以使用collapse()方法将较小的集合合并为较大的集合,从而完成chunk()的相反操作。 在这里查看

遍历数据 ( Iterating through Data )

map (map)

The map function iterates through a collection and subjects each value to a callback function.

map函数遍历一个集合,并将每个值置于一个回调函数中。

We will create a collection of peoples names and return a collection of the length of each of the names.

我们将创建一个民族名称集合,并返回每个名称长度的集合。

/**
     * map function iterates a collection through a callback
     * function and performs an operation on each value.
     */
    public function mapMe()
    {
        $names = collect([
            'Albert', 'Ben', 'Charles', 'Dan', 'Eric', 'Xavier', 'Yuri', 'Zane'
        ]);

        $lengths = $names->map(function ($name, $key) {
            return strlen($name);
        });

        $lengths->toArray();
        //[6, 3, 7, 3, 4, 6, 4, 4,]
    }

transform (transform)

While the map method creates a new collection, sometimes you would want to edit the original collection. The transform method takes a callback method and performs an action on the same collection.

当map方法创建一个新的集合时,有时您会想要编辑原始的集合。 transform方法采用回调方法,并对同一个集合执行操作。

Since transforming does not create a new collection, you do not need to assign it to a new value.

由于转换不会创建新集合,因此您无需将其分配给新值。

/**
     * Transform perfoms an action on an original collection.
     */
    public function transformMe()
    {
        $names = collect([
            'Albert', 'Ben', 'Charles', 'Dan', 'Eric', 'Xavier', 'Yuri', 'Zane'
        ]);

        $names->transform(function ($name, $key) {
            return strlen($name);
        });

        $names->toArray();
        //[6, 3, 7, 3, 4, 6, 4, 4,]
    }

reduce (reduce)

Unlike the map and transform methods, the reduce method returns a single value. It passes the result of each iteration to the next iteration.

与map和transform方法不同,reduce方法返回单个值。 它将每个迭代的结果传递到下一个迭代。

For instance, to get the sum of integer values in a collection, reduce passes the sum of subsequent numbers and performs an addition of the result to the next number iteratively.

例如,要获得集合中整数值的总和,reduce将后续数字的总和传递给迭代数,然后将结果累加到下一个数字。

/**
     * Get the sum of numbers in a collection
     */
    public function reduceMe()
    {
        $numbers = collect([
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10
        ]);

        $sum = $numbers->reduce(function ($sum, $number) {
            return $sum + $number;
        });
        //55
    }

each (each)

The each method passes each of the items through a callback function.

each方法通过回调函数传递每个项目。

The most interesting section about the each method is that you can easily break out of the iteration by simply returning false in the callback function.

关于每种方法的最有趣的部分是,您只需在回调函数中返回false即可轻松退出迭代。

/**
     * Print out a list of numbers that are lesser than or
     * equal to five
     */
    public function eachMethod()
    {
        $numbers = collect([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
        $smallNumbers = $numbers->each(function ($num, $key) {
            if ($num > 5) {
                return false;
            }
            echo $num .", ";
        });
        //1, 2, 3, 4, 5,
    }

every (every)

The every method creates a new collection that is made up of every n-th element in a collection.

每个方法都会创建一个新集合,该集合由集合中的每个第n个元素组成。

将集合用作集合 ( Using A Collection As A Set )

The collection class also provides methods that help us work with data as sets. This means we can compare two data sets and perform actions based on that.

集合类还提供了一些方法来帮助我们将数据作为集合进行处理。 这意味着我们可以比较两个数据集并据此执行操作。

union (union)

The union() method is used to add values to a collection from an array. If a value already exists in the collection, the value in the array is ignored.

union()方法用于将值添加到数组的集合中。 如果集合中已经存在一个值,则将忽略数组中的值。

/**
     * add array values to a collection using union
     */
    public function union()
    {
        $coolPeople = collect([
            1 => 'John', 2 => 'James', 3 => 'Jack'
        ]);

        $allCoolPeople = $coolPeople->union([
            4 => 'Sarah', 1 => 'Susan', 5 =>'Seyi'
        ]);
        $allCoolPeople->all();
        /*
        [
            1 => "John", 2 => "James", 3 => "Jack", 4 => "Sarah", 5 => "Seyi",
       ]
       */
    }

intersect (intersect)

The intersect() method removes the elements in a collection that are not in a passed array or collection.

intersect()方法删除集合中不包含在传递的数组或集合中的元素。

/**
     * Return a list of very cool people in collection that
     * are in the given array
     */
    public function intersect()
    {
        $coolPeople = collect([
            1 => 'John', 2 => 'James', 3 => 'Jack'
        ]);

        $veryCoolPeople = $coolPeople->intersect(['Sarah', 'John', 'James']);
        $veryCoolPeople->toArray();
        //[1 => "John" 2 => "James"]
    }

You will notice that the intersect method preserves the keys for the returned values.

您会注意到,intersect方法保留了返回值的键。

结论 ( Conclusion )

I have tried to cover most of the collection methods that you might find yourself using but there is still much left out there to learn.

我已经尝试介绍了您可能会发现自己使用的大多数收集方法,但是仍然有很多东西需要学习。

Most notably, I left out the following

最值得注意的是,我省略了以下内容

There is still much more on the Laravel documentation and the Laravel API documentation that you can do with collections that you may want to take a look at.

Laravel文档Laravel API文档中还有很多您可以看一下的集合。

To follow up on this tutorials code, check out the github repo here. Feel free to make your contributions on it.

要继续学习本教程代码,请在此处查看github仓库。 随时为此做出您的贡献。

翻译自: https://scotch.io/tutorials/laravel-collections-php-arrays-on-steroids

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值