【问题标题】:Displaying results in descending order of value within array variable在数组变量中按值的降序显示结果
【发布时间】:2013-04-12 02:23:19
【问题描述】:

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

【问题讨论】:

    标签: php mysql arrays sorting


    【解决方案1】:
    $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

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

    【讨论】:

    • 尝试删除 "" 并没有任何改变:/ 仍然以随机顺序出现
    【解决方案2】:
    // 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

    【讨论】:

    • 现在在那里尝试过,没有任何乐趣:/我知道它很简单,但我无法理解它
    • 谢谢你,它与我的结果一起工作,现在我的输出是 32->footballer 50->IT 53->hairdresser 67->nursing 77->dentist 我如何将其更改为牙医 77% 的超链接?
    • 在 foreach 循环中,$item 是一个基于 0 的索引数组(如 $dentist 等)。 -> 回声 $item[0].' '.$item[1].' '.$item[2].' '.$item[3].'
      ';显示顺序与索引顺序相同,所以也可以使用 echo implode(' ', $item).'
      ';
    • 嗯,我不太清楚你的意思,你能再解释一下吗?这个 PHP 东西相当新。感谢您的宝贵时间
    • $items 包含现在按结果值排序的 5 个数组($dentist 等)。在 foreach ($items as $item) { ... } 中,$item 表示当前的“结果”数组(即 $dentist、$IT 等)。例如,显示当前项目的链接值: echo $item[3];因为链接是每个原始数组中的第四个值。
    猜你喜欢
    • 2015-09-23
    • 2013-10-30
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    相关资源
    最近更新 更多