0

嗨,我想知道是否有人可以帮助我。我创建了一份问卷,问卷中有多个结果(显示为%)。我能够在 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";
}
?>
4

2 回答 2

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

http://www.php.net/manual/fr/function.usort.php

于 2013-04-18T20:00:54.220 回答
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

(*编写一个比较函数并不像看起来那么困难)

于 2013-04-18T20:00:14.470 回答