我想在我的VIEWS中的foreach循环中以$ distance为基础对它进行排序。因此,这是我在Models中的代码
$db = $this->getDbo();
$query = $db->getQuery(true)
->select('*')
->from('#__load');
$db->setQuery($query);
$db->query();
$rows = $db->loadObjectList();
return $db->loadObjectList();
这是我要按距离对它进行排序的视图中的代码
foreach ($this->items as $i => $item) {
$distance = $item->result1 * $item->result2
sort($distance)
}
echo $distance
结果
3、6、2、7、8
我想这样显示
2,3,6,7,8
sort在数组上起作用,而你正在做的是对数组中将无法起作用的每个项目调用sort。
您可以做的是做您的foreach循环,然后排序:
$array = [];
foreach ($this->items as $i => $item) {
$distance = $item->result1 * $item->result2;
$array[] = $distance;
}
sort($array);
var_dump($array);
首先将结果$this->items
转换为(array)$this->items
,然后使用以下功能之一:
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
并获得排序的值。