11

My object array ($c) like this :

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

If in php normal I try like this :

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?

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

How can I solve it?

2
  • Although there are solutions provided for sorting an array with objects in it, I would suggest you to take a look, why you are getting such structure in the first place. Laravel has built in toArray methods for most cases, so you can convert objects to arrays on the calls. Commented Jun 18, 2017 at 8:26
  • Using usort in php with a class private function eval.in/818360 Commented Jun 18, 2017 at 8:36

3 Answers 3

23

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

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 methods you can work with. In your case, you need the 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:

$c = $c->all();

// or

$c = $c->toArray();
Sign up to request clarification or add additional context in comments.

2 Comments

It works. But the result : Array ( [3] => stdClass Object ( [id] => 1 [name] => city.png ) [0] => stdClass Object ( [id] => 3 [name] => chelsea.png ) [1] => stdClass Object ( [id] => 4 [name] => arsenal.png ) [2] => stdClass Object ( [id] => 5 [name] => mu.png ) ). The index : 3 -> 0 -> 1 ->2. I want the index also change. Should like this : 0->1->2->3
@TrendingNews Use values() method on the result, like this: $c = $c->values()->toArray(); Read about the values() method here.
3

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 sortById method

Comments

2

If you want a pure php way:

In your first case you are trying to pass a procedural function as a callback to your usort function.

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

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

Checkout the third example from the usort man page.

Note that, you will using the $this variable 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:

$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']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.