I can't figure out why asort isn't working. Neither does any other sort work. $hs['hs_type'] are values that come from MySQL query.
我无法弄清楚为什么asort不起作用。也没有任何其他类型的工作。 $ hs ['hs_type']是来自MySQL查询的值。
$results = $query->result_array();
$hs_types = array();
foreach($results as $hs) {
$hs_types[$hs['hs_type']]++;
}
$projects = array();
foreach($hs_types as $hs) {
array_push($projects, $hs);
}
asort($projects);
var_dump for my array before sort: array (size=15)
排序前我的数组的var_dump:array(size = 15)
* 8 => int 1709
* 13 => int 26
* 7 => int 474
* 14 => int 800
* 11 => int 282
* 6 => int 61
* 5 => int 23
* 15 => int 181
* 3 => int 2
* 19 => int 3
* 9 => int 50
* 1 => int 44
* 2 => int 2
* 4 => int 4
* 18 => int 13
var_dump for my array after sort: array (size=15)
排序后我的数组的var_dump:array(size = 15)
* 8 => int 2
* 12 => int 2
* 9 => int 3
* 13 => int 4
* 14 => int 13
* 6 => int 23
* 1 => int 26
* 11 => int 44
* 10 => int 50
* 5 => int 61
* 7 => int 181
* 4 => int 282
* 2 => int 474
* 3 => int 800
* 0 => int 1709
What I wanted:
我想要的:
* 3 => int 2
* 2 => int 2
* 19 => int 3
* 4 => int 4
* 18 => int 13
* 5 => int 23
* 13 => int 26
* 1 => int 44
* 9 => int 50
* 15 => int 181
* 11 => int 282
* 7 => int 474
* 14 => int 800
* 8 => int 1709
3 个解决方案
#1
1
Problem is that you are getting everything into your nicely keyed array, then pushing each item into the $projects array (losing the key) then doing a asort on the $projects array.
问题是你将所有东西都放入你的键控数组中,然后将每个项目推入$ projects数组(丢失键),然后在$ projects数组上执行asort。
It shows up with this minor test script:-
它显示了这个小测试脚本: -
<?php
$hs_types = array(8 => 1709,
13 => 26,
7 => 474,
14 => 800,
11 => 282,
6 => 61,
5 => 23,
15 => 181,
3 => 2,
19 => 3,
9 => 50,
1 => 44,
2 => 2,
4 => 4,
18 => 13);
Echo "\r\nOriginal array\r\n";
print_r($hs_types);
$projects = array();
foreach($hs_types as $hs)
{
array_push($projects, $hs);
}
asort($projects);
Echo "\r\nPushed array sorted\r\n";
print_r($projects);
asort($hs_types);
Echo "\r\nOriginal array sorted\r\n";
print_r($hs_types);
?>
but it looks like it would be easier to just get the sorted list in SQL in the first place.
但看起来首先在SQL中获取排序列表会更容易。
#2
0
I tried using your array with asort() and it worked perfectly, coming back with the result you expected:
我尝试使用你的数组与asort(),它完美地工作,回来与你期望的结果:
<?php
//your array
$a = array(
8 => 1709,
13 => 26,
7 => 474,
14 => 800,
11 => 282,
6 => 61,
5 => 23,
15 => 181,
3 => 2,
19 => 3,
9 => 50,
1 => 44,
2 => 2,
4 => 4,
18 => 13
);
//print unsorted array
print_r($a);
//sort the array
asort($a);
//print the sorted array
echo "<br/><br/>";
print_r($a);
If this really isn't working for you, why not do the sorting in the SQL query instead? Then you wouldn't need to bother re-sorting it in PHP.
如果这对您不起作用,为什么不在SQL查询中进行排序呢?然后你不需要在PHP中重新排序它。
#3
0
Are you sure you are using asort
instead of sort
? I tried your code and it works perfectly :
你确定你使用的是asort而不是sort吗?我尝试了你的代码,它完美地工作:
<?php
$myArray = array(
8 => 1709,
13 => 26,
7 => 474,
14 => 800,
11 => 282,
6 => 61,
5 => 23,
15 => 181,
3 => 2,
19 => 3,
9 => 50,
1 => 44,
2 => 2,
4 => 4,
18 => 13,
);
var_dump($myArray);
asort($myArray);
var_dump($myArray);
?>
My result :
我的结果:
#1
1
Problem is that you are getting everything into your nicely keyed array, then pushing each item into the $projects array (losing the key) then doing a asort on the $projects array.
问题是你将所有东西都放入你的键控数组中,然后将每个项目推入$ projects数组(丢失键),然后在$ projects数组上执行asort。
It shows up with this minor test script:-
它显示了这个小测试脚本: -
<?php
$hs_types = array(8 => 1709,
13 => 26,
7 => 474,
14 => 800,
11 => 282,
6 => 61,
5 => 23,
15 => 181,
3 => 2,
19 => 3,
9 => 50,
1 => 44,
2 => 2,
4 => 4,
18 => 13);
Echo "\r\nOriginal array\r\n";
print_r($hs_types);
$projects = array();
foreach($hs_types as $hs)
{
array_push($projects, $hs);
}
asort($projects);
Echo "\r\nPushed array sorted\r\n";
print_r($projects);
asort($hs_types);
Echo "\r\nOriginal array sorted\r\n";
print_r($hs_types);
?>
but it looks like it would be easier to just get the sorted list in SQL in the first place.
但看起来首先在SQL中获取排序列表会更容易。
#2
0
I tried using your array with asort() and it worked perfectly, coming back with the result you expected:
我尝试使用你的数组与asort(),它完美地工作,回来与你期望的结果:
<?php
//your array
$a = array(
8 => 1709,
13 => 26,
7 => 474,
14 => 800,
11 => 282,
6 => 61,
5 => 23,
15 => 181,
3 => 2,
19 => 3,
9 => 50,
1 => 44,
2 => 2,
4 => 4,
18 => 13
);
//print unsorted array
print_r($a);
//sort the array
asort($a);
//print the sorted array
echo "<br/><br/>";
print_r($a);
If this really isn't working for you, why not do the sorting in the SQL query instead? Then you wouldn't need to bother re-sorting it in PHP.
如果这对您不起作用,为什么不在SQL查询中进行排序呢?然后你不需要在PHP中重新排序它。
#3
0
Are you sure you are using asort
instead of sort
? I tried your code and it works perfectly :
你确定你使用的是asort而不是sort吗?我尝试了你的代码,它完美地工作:
<?php
$myArray = array(
8 => 1709,
13 => 26,
7 => 474,
14 => 800,
11 => 282,
6 => 61,
5 => 23,
15 => 181,
3 => 2,
19 => 3,
9 => 50,
1 => 44,
2 => 2,
4 => 4,
18 => 13,
);
var_dump($myArray);
asort($myArray);
var_dump($myArray);
?>
My result :
我的结果: