laravel 如何在laravel上对对象进行排序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44612789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:09:47  来源:igfitidea点击:

How can I sort object on laravel?

phplaravelsortinglaravel-5.3

提问by Success Man

My object array ($c) like this :

我的对象数组 ($c) 像这样:

Array
(
    [0] => stdClass Object
        (
            [id] => 3
            [name] => chelsea.png
        )

    [1] => stdClass Object
        (
            [id] => 4
            [name] => arsenal.png
        )

    [2] => stdClass Object
        (
            [id] => 5
            [name] => mu.png
        )

    [3] => stdClass Object
        (
            [id] => 1
            [name] => city.png
        )

)

I want to sort it by id

我想按id排序

If in php normal I try like this :

如果在 php 正常我尝试这样:

function sort_by_id( $a, $b )
{ 
  if(  $a->id ==  $b->id ){ return 0 ; } 
  return ($a->id < $b->id) ? -1 : 1;
} 
usort($c,'sort_by_id');  
echo '<pre>';print_r($c);echo '</pre>';

It works

有用

But How can I implement it use laravel?

但是我如何使用laravel来实现它?

I try like this :

我尝试这样:

public function add($param)
{
    ...
    usort($c, $this->sortById()); 
    echo '<pre>';print_r($c);echo '</pre>';die();
    ...
}

private function sortById($a, $b)
{ 
    if($a->id == $b->id) { 
        return 0 ; 
    } 
    return ($a->id < $b->id) ? -1 : 1;
} 

It does not work

这是行不通的

There exist error like this :

存在这样的错误:

Undefined property ... $sortById ...

未定义的属性 ... $sortById ...

How can I solve it?

我该如何解决?

回答by lesssugar

Don't reinvent the wheel. Laravel is a fantastic framework that did lots of work for you already.

不要重新发明轮子。Laravel 是一个很棒的框架,它已经为你做了很多工作。

First, you need to convert your array to a Collection:

首先,您需要将数组转换为集合:

$c = collect($c);

Now, For working with collections, you have a nice set of available methodsyou can work with. In your case, you need the sortBy():

现在,对于使用集合,您有一组很好的可用方法可以使用。在您的情况下,您需要sortBy()

$sorted = $c->sortBy('id');

Alternatively, you can pass a callback:

或者,您可以传递回调:

$sorted = $c->sortBy(function ($item, $key) {
    return $item->id;
});

Both of these will return a Collection object. If you want to get an Array back instead, use all()or toArray()on your result:

这两个都将返回一个 Collection 对象。如果您想取回数组,请在结果上使用all()toArray()

$c = $c->all();

// or

$c = $c->toArray();

回答by Nazmul Hasan

Try this

尝试这个

public function add($param)
{
    ...
    usort($c, function($a, $b){
        if($a->id == $b->id) { 
        return 0 ; 
    } 
    return ($a->id < $b->id) ? -1 : 1;
    }); 
    echo '<pre>';print_r($c);echo '</pre>';die();
    ...
}

You don't need to use different sortByIdmethod

你不需要使用不同的sortById方法

回答by hassan

If you want a pure php way:

如果你想要一个纯 php 的方式:

In your first case you are trying to pass a procedural functionas a callback to your usortfunction.

在您的第一种情况下,您试图将过程函数作为回调传递给您的usort函数。

When you want to pass a methodfrom a class as a callback you need to pass it as follows:

当您想将类中的方法作为回调传递时,您需要按如下方式传递它:

usort($c, [$this, 'sortById']);

Checkout the third example from the usortman page.

查看usort手册页中的第三个示例。

Note that, you will using the $thisvariable as long as you are within the class. If you want to use the context of another class for example, you will need to pass the instance as follows:

请注意,$this只要您在班级内,就会使用该变量。例如,如果要使用另一个类的上下文,则需要按如下方式传递实例:

$object = new someClass;
usort($c, [$object, 'sortById']);

Also in case if you need to pass a static method you can simply pass it as follows:

另外,如果您需要传递静态方法,您可以简单地按如下方式传递它:

usort($c, ['someClass', 'sortById']);