只是想问一下我是如何使用网站名称重新排序数组中的数据?我想在站点名称循环时一次只显示一个数据。
例如网站订单是这个
site1
site2
site3
site4
我想要的是它,它必须按最高费率或数字排序:
name: John Doe
site: site1
rate: 10
name: Joen Doe
site: site2
rate: 10
name: Ejon Odeo
site: Site3
rate: 9.7
name: Ejosan Odeo
site: Site4
rate: 9.5
name: John Smith
site: site1
rate: 9.3
name: Jodsen Dsoe
site: site2
rate: 8
name: Efsdjon Oddeo
site: Site3
rate: 6.3
依旧......
它会循环直到它全部打印出来。
名称数组:
Array ( [0] => Array ( [name] => name here [rate] => 10 [site] => site here)
Array ( [1] => Array ( [name] => name here [rate] => 9 [site] => site here)
Array ( [2] => Array ( [name] => name here [rate] => 8 [site] => site here)
Array ( [3] => Array ( [name] => name here [rate] => 7 [site] => site here)
Array ( [4] => Array ( [name] => name here [rate] => 6 [site] => site here)
Array ( [5] => Array ( [name] => name here [rate] => 5 [site] => site here)
Array ( [6] => Array ( [name] => name here [rate] => 5 [site] => site here)
网站数组 我想在数组中的每个站点名称中只显示一个人,当站点阵列到达终点时,它将再次以 exs 站点名称开始,直到人员阵列全部显示出来。
Array ( [0] => Array ( [acronym] => exs [site_order] => 1 ) [1] => Array ( [acronym] => ts [site_order] => 1 ) [2] => Array ( [acronym] => ih [site_order] => 2 ) [3] => Array ( [acronym] => tp [site_order] => 3 ) [4] => Array ( [acronym] => tc [site_order] => 4 ) [5] => Array ( [acronym] => cfnm [site_order] => 5 ) [6] => Array ( [acronym] => sn [site_order] => 6 ) [7] => Array ( [acronym] => tla [site_order] => 7 ) [8] => Array ( [acronym] => bsc [site_order] => 8 ) [9] => Array ( [acronym] => tdp [site_order] => 9 ) [10] => Array ( [acronym] => lhd [site_order] => 10 ) [11] => Array ( [acronym] => ss [site_order] => 11 ) [12] => Array ( [acronym] => pov [site_order] => 12 ) [13] => Array ( [acronym] => rat [site_order] => 13 ) [14] => Array ( [acronym] => trw [site_order] => 14 ) [15] => Array ( [acronym] => tgs [site_order] => 15 ) [16] => Array ( [acronym] => tlm [site_order] => 16 ) [17] => Array ( [acronym] => ol [site_order] => 17 ) [18] => Array ( [acronym] => tb [site_order] => 18 ) [19] => Array ( [acronym] => ta [site_order] => 19 ) [20] => Array ( [acronym] => hfy [site_order] => 20 ) [21] => Array ( [acronym] => sd [site_order] => 21 ) [22] => Array ( [acronym] => si [site_order] => 22 ) [23] => Array ( [acronym] => tse [site_order] => 23 ) )
站点阵列中的首字母缩写词是站点名称。请帮助我们
答案 0 :(得分:1)
查看usort
http://php.net/manual/en/function.usort.php
<?php
// your example array
$data = array(
0 => array('name' => 'name 1', 'rate' => 1, 'site' => 'site 8'),
1 => array('name' => 'name 2', 'rate' => 2, 'site' => 'site 9'),
2 => array('name' => 'name 3', 'rate' => 4, 'site' => 'site 10'),
3 => array('name' => 'name 4', 'rate' => 3, 'site' => 'site 11'),
4 => array('name' => 'name 5', 'rate' => 6, 'site' => 'site 12'),
5 => array('name' => 'name 6', 'rate' => 7, 'site' => 'site 13'),
6 => array('name' => 'name 7', 'rate' => 5, 'site' => 'site 14')
);
// sort array with user defined function, see below
usort($data, 'sortByRate');
// now you can walk through the array and print it
foreach ($data as $item) {
echo 'name: '.$item['name'].'<br>';
echo 'site: '.$item['site'].'<br>';
echo 'rate: '.$item['rate'].'<br>';
echo '<br>';
}
// function wich sorts the array
function sortByRate($a, $b) {
if ($a['rate'] == $b['rate']) {
return 0;
}
return ($a['rate'] > $b['rate']) ? -1 : 1;
}
答案 1 :(得分:0)
如果我正确理解了您的问题,您希望按一个值对多维数组进行排序。 This question has been asked before。对于您的具体情况,您可以像这样更改代码(使用PHP 5.3及更高版本):
usort($someArray, function($a, $b) {
return $a['rate'] - $b['rate'];
});