0

I want sorting Teams list by "terminland_days" value

I read same question in Stack Overflow https://stackoverflow.com/a/38237197/5006328 but still have problem on this.

This is my PHP (PHP >=7.4) code:

public function display($tpl = null) {
// myval get from other model

//print_r ($myval);
$myval = Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [state] => 1
            [teams] => {"teams0":{"name":"Jhon","terminland_days":"3"},"teams1":{"name":"Alex","terminland_days":"2"}} 
            [distance] => 5839.5147520164
        )

    [1] => stdClass Object
        (
            [id] => 2
            [state] => 1
            [teams] => {"teams0":{"name":"Jeff","terminland_days":"12"},"teams1":{"name":"Fred","terminland_days":"1"}} 
            [distance] => 5839.5147520164
        )

);

foreach ($myval as $item) {

    $paramsteam = json_decode($item->teams);
    
    foreach ($paramsteam as $team) {
        // i want sorting Teams as "terminland_days" value
        usort($team, "compare_func");
        // error ==> Warning: usort() expects parameter 1 to be array, object given
        echo $team->terminland_days;
        
    }

}

}
public function compare_func($a, $b)
{
    if ($a->terminland_days == $b->terminland_days) {
        return 0;
    }
    return ($a->terminland_days < $b->terminland_days) ? -1 : 1;
    // You can apply your own sorting logic here.
}
    

as I understand, I must use usort, please help me how I can do it?

When print_r ($team); output:

stdClass Object
(
[name] => Jhon
[terminland_days] => 3
)
stdClass Object
(
[name] => Alex
[terminland_days] => 2 
)
stdClass Object
(
[name] => Jeff
[terminland_days] => 12
)
stdClass Object
(
[name] => Fred
[terminland_days] => 1
)
Dharman
  • 30,962
  • 25
  • 85
  • 135
Amin
  • 35
  • 6
  • Please share more details. What **exactly** is not working with the given code? What have you tried to resolve the problem? – Nico Haase Jan 28 '22 at 12:54
  • Also, can you share a runnable example of your code? What does `$team` contain? – Nico Haase Jan 28 '22 at 12:54
  • @NicoHaase I put `print_r ($team);` output on Question post. please check. thank you – Amin Jan 28 '22 at 13:44
  • Well, then the problem is obvious: `usort` sorts arrays. What's the point in sorting a **single** object? – Nico Haase Jan 28 '22 at 13:45
  • @NicoHaase So your suggestion for solving the problem? – Amin Jan 28 '22 at 13:55
  • That depends on what you want to achieve. A single object cannot be ordered. Maybe you want to sort the list of **all** teams, like `usort($paramsteam, "compare_func")`? – Nico Haase Jan 28 '22 at 14:20
  • @NicoHaase not working `usort($paramsteam, "compare_func")` – Amin Jan 28 '22 at 16:01
  • "Not working" is pretty broad. What does that mean? What have you tried to make it work? – Nico Haase Jan 28 '22 at 16:21
  • @NicoHaase i get this error `Warning: usort() expects parameter 1 to be array, object given` when put `usort($paramsteam, "compare_func")` before this line `foreach ($paramsteam as $team) ` – Amin Jan 28 '22 at 16:47
  • Please add all clarification to your question by editing it. Also, please explain what you want to achieve – Nico Haase Jan 28 '22 at 20:37

1 Answers1

0

After a few hours of scrutiny, I realized that the best way was to first convert the object to an array and then sort it. so :

$paramsteam = json_decode($item->teams,true);
usort($paramsteam, function ($item1, $item2) {
return $item1['terminland_days'] <=> $item2['terminland_days'];
});
foreach ($paramsteam as $team) {
    
    echo $team['terminland_days'];
    
}

also @Nico haase thank you

Amin
  • 35
  • 6