[英]How to sort SQL query result by $value in PHP associative array?
I have an array which is the result of an SQL query. 我有一个数组,它是SQL查询的结果。 When I
print_r($results)
I get 当我
print_r($results)
我得到
Array ( [0] => Array ( [Q1] => 24 ) ) Array ( [0] => Array ( [Q2] => 12 ) ) Array ( [0] => Array ( [Q3] => 14 ) ) Array ( [0] => Array ( [Q4] => 10 ) ) etc.
When I display contents in HTML with a foreach ($result as $key=>$value)
loop, I get 当我使用
foreach ($result as $key=>$value)
循环在HTML中显示内容时,我得到了
-----------
|Q1 | 24|
|Q2 | 12|
|Q3 | 14|
|Q4 | 10|
-----------
But I'd like to sort the array by value so that I get 但是我想按值对数组排序,以便得到
-----------
|Q4 | 10|
|Q2 | 12|
|Q3 | 14|
|Q1 | 24|
-----------
The second column is the product of a calculation, and not an actual DB column, so I can't use ORDER BY
in the SQL. 第二列是计算的结果,而不是实际的DB列,因此我不能在SQL中使用
ORDER BY
。 Here's the query itself: 这是查询本身:
SELECT (SUM(Q".$i.")) AS Q$i FROM marks
I've tried sort functions - arsort($results)
and others. 我试过排序函数
arsort($results)
和其他函数。 I've tried reassigning to 2D arrays - $final_result[] = $results;
我尝试过重新分配到2D数组-
$final_result[] = $results;
and sorting - but no luck. 和排序-但没有运气。
Is there some trick I'm missing in my attempt to access the actual values in $results? 我尝试访问$ results中的实际值时是否缺少一些技巧? (24, 12, 14 etc)
(24、12、14等)
With a combination of usort
(user-function sort), strcmp
(string comparison) and key
(for the variable keys) you can handle this: 通过结合使用
usort
(用户功能排序), strcmp
(字符串比较)和key
(对于可变键),您可以处理此问题:
$quarters = array(
array(
'Q1' => 24
),
array(
'Q2' => 12
),
array(
'Q3' => 14
),
array(
'Q4' => 10
)
);
usort($quarters, function($a, $b) {
return reset($a) - reset($b);
});
print_r($quarters);
Gives us: 给我们:
Array
(
[0] => Array
(
[Q4] => 10
)
[1] => Array
(
[Q2] => 12
)
[2] => Array
(
[Q3] => 14
)
[3] => Array
(
[Q1] => 24
)
)
I found a way to sort the results of my query. 我找到了一种对查询结果进行排序的方法。 It's a workaround.
这是一种解决方法。
The problem was that my query SELECT FLOOR(SUM(Q".$i.")) AS Q$i FROM table
didn't yield an associative array that asort(), usort()
etc could handle. 问题是我的查询
SELECT FLOOR(SUM(Q".$i.")) AS Q$i FROM table
没有产生asort(), usort()
等可以处理的关联数组。
When I did a var_export($results)
it output: 当我执行
var_export($results)
,输出:
array ( 0 => array ( 'Q1' => '24', ), )array ( 0 => array ( 'Q2' => '12', ), )array ( 0 => array ( 'Q3' => '14', ), )array ( 0 => array ( 'Q4' => '10', ), )
Note the non-incrementing 0; 注意非增量0; this seems to be why the sort algorithms aren't working.
这似乎就是为什么排序算法不起作用的原因。
However, I was able to display the query results in a foreach loop without a problem to give: 不过, 我可以不给的问题,以显示在foreach循环查询结果:
-----------
|Q1 | 24|
|Q2 | 12|
|Q3 | 14|
|Q4 | 10|
-----------
So my solution was to reassign the query results to an associative array in that loop, as follows: 因此, 我的解决方案是将查询结果重新分配给该循环中的关联数组,如下所示:
foreach ($results as $result)
{
foreach ($result as $key=>$value)
{
$rankings["$key"]="$value";
}
}
}
Now, a var_export($rankings)
reveals the data structure I would expect: 现在,一个
var_export($rankings)
揭示了我期望的数据结构:
array ( 'Q1' => '24', 'Q2' => '12', 'Q3' => '14', 'Q4' => '10', )
And this new array was amenable to sorting: 而这个新的数组是服从排序:
asort($rankings);
-----------
|Q4 | 10|
|Q2 | 12|
|Q3 | 14|
|Q1 | 24|
-----------
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.