简体   繁体   English

对 foreach 循环结果进行排序

[英]Sort foreach loop result

I want to sort it by $distance base in foreach loop in my VIEWS.. so heres my code in Models我想在我的 VIEWS 中按 foreach 循环中的 $distance base 对其进行排序。所以这是我在 Models 中的代码

    $db = $this->getDbo();
    $query = $db->getQuery(true)
                ->select('*')
                ->from('#__load');

    $db->setQuery($query);               
    $db->query();
    $rows = $db->loadObjectList();
    return $db->loadObjectList();

This is the code in my View where i want to sort it by distance这是我的视图中的代码,我想按距离对其进行排序

  foreach ($this->items as $i => $item) {  
       $distance = $item->result1 * $item->result2        
  sort($distance)
}

echo $distance

result结果

3, 6, 2, 7, 8 3、6、2、7、8

i want to show like this我想这样展示

2, 3, 6, 7, 8 2、3、6、7、8

sort works on an array, and what you are doing is you are calling sort on every item in the array which wont work. sort 适用于数组,而您正在做的是对数组中不起作用的每个项目调用 sort 。

What you can do instead is do your foreach loop and then sort after:你可以做的是做你的 foreach 循环,然后排序:

  $array = [];
  foreach ($this->items as $i => $item) {  
      $distance = $item->result1 * $item->result2;        
      $array[] = $distance;
  }
  sort($array);
  var_dump($array);

https://www.php.net/manual/en/function.sort.php https://www.php.net/manual/en/function.sort.php

First Convert your result $this->items into (array)$this->items and then use one of the following function:首先将结果$this->items转换为(array)$this->items ,然后使用以下 function 之一:

sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order, according to the value
ksort() - sort associative arrays in ascending order, according to the key
arsort() - sort associative arrays in descending order, according to the value
krsort() - sort associative arrays in descending order, according to the key

and get sorted value.并获得排序值。

There are a few things here that don't make sense to me, so I'll blindly try to refactor your scripts and put all of the processing in the model where it belongs (you shouldn't be manipulating data in the view).这里有几件事对我来说没有意义,所以我会盲目地尝试重构你的脚本并将所有处理放在它所属的 model 中(你不应该在视图中操作数据)。

Untested snippet:未经测试的片段:

$db = $this->getDbo();
$query = $db->getQuery(true)
            ->select('result1 * result2')
            ->from('#__load')
            ->orderBy(1);
$db->setQuery($query);               
return $db->loadColumn();

Relevant pages to read:相关页面阅读:

I expect that your view will now receive the following sorted and indexed array:我希望您的视图现在将收到以下排序和索引的数组:

$this->items = [2, 3, 6, 7, 8];

If you are a Joomla user, come join us at Joomla Stack Exchange .如果您是 Joomla 用户,请加入我们Joomla Stack Exchange Have a browse of my answers to mysql tagged questions for explained examples and best practices.浏览我对mysql标记问题的回答,了解示例和最佳实践。

If you are living in Brisvegas, come to our monthly Joomla User Group Meetup in West End (we aren't scary people).如果您住在布里斯维加斯,请参加我们在西区举行的每月 Joomla 用户组聚会(我们不是可怕的人)。 This is a place where you can leverage an IRL network of people that want to help you grow your skills and get your projects finished.在这里,您可以利用 IRL 人脉网络来帮助您提高技能并完成项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM