嗨,我想知道是否有人可以帮助我。我创建了一份调查问卷,调查问卷输出了多个结果(显示为%)。我可以在results.php页面上显示这些结果,以及相应的名称($ careername)和相应的链接($ link)。例如。我有5个结果,每个结果链接到相应的职业和链接,这些结果显示在results.php页面上。但是我需要结果,职业名称和链接以结果值的降序显示。截至目前,它们以随机顺序显示。以下是我正在使用的代码,如果有人有任何想法,我将不胜感激。
<?php
$careername1 = 'Nursing ';
$careername2 = 'Footballer ';
$careername3 = 'Dentist ';
$careername4 = 'Hairdressing ';
$careername5 = 'IT ';
?>
<?php
$link1 = '<br><a href="http://www.nhscareers.nhs.uk/explore-by-career/nursing/" target="_blank">More information on Nursing</a></br></br>';
$link2 = '<br><a href="#" target="_blank">More information on Footballing</a></br> </br>';
$link3 = '<br><a href="#" target="_blank">More information on Dentistry</a></br></br>';
$link4 = '<br><a href="#" target="_blank">More information on Hairdressing</a></br></br>';
$link5 = '<br><a href="#" target="_blank">More information on IT</a></br></br>';
?>
<?php
$nursing = array($careername1, $result1, "% ", $link1);
$footballer = array($careername2, $result2, "% ", $link2);
$dentist = array($careername3, $result3, "% ", $link3);
$hairdresser = array($careername4, $result4, "% ", $link4);
$IT = array($careername5, $result5, "% ", $link5);
?>
<h1>Your results are listed below:</h1>
<?php
$items = array("$nursing", "$footballer", "$dentist", "$hairdresser", "$IT");
arsort($items);
foreach ($items as $key => $val) {
echo "$key = $val\n";
}
?>
答案 0 :(得分:1)
// some test values
$nursing = array("nursing", 5, "% ", "");
$footballer = array("footballer", 15, "% ","");
$dentist = array("dentist", 25, "% ", "");
$hairdresser = array("hairdresser", 0, "% ", "");
$IT = array("IT", 50, "% ", "");
$items = array($nursing, $footballer, $dentist, $hairdresser, $IT);
foreach ($items as $item) {
echo $item[1].'->'.$item[0].'<br>';;
}
输出未排序
5->nursing
15->footballer
25->dentist
0->hairdresser
50->IT
按索引1降序排序
function compare($a, $b) {
if ($a[1] == $b[1]) {
return 0;
}
return ($a[1] < $b[1]) ? -1 : 1;
}
usort($items, 'compare');
foreach ($items as $item) {
echo $item[1].'->'.$item[0].'<br>';;
}
输出排序
0->hairdresser
5->nursing
15->footballer
25->dentist
50->IT
答案 1 :(得分:0)
$items = array("$nursing", "$footballer", "$dentist", "$hairdresser", "$IT");
请注意,"$nursing"
应为$nursing
等。
$items = array($nursing, $footballer,$dentist, $hairdresser,$IT);
如果这不起作用,您可能需要编写自己的compare
函数并将其用作arsort
函数的第二个参数。
有关详细信息,请阅读http://php.net/manual/en/function.arsort.php
(*编写比较函数并不像看起来那么困难)