0

我希望echo按 的值排序$match_count。最高的数字应该在顶部。

while($row = mysql_fetch_array($result)) {
    $num_kanal = count(explode(' ', $row['kanaler']));

    $pr_kanal = $row['pris'] / $num_kanal;
    $pr_kanal = number_format((float)$pr_kanal, 2, '.', '');

    $farve = '#6a6a6a;';

    if ($_POST['kanaler']) {

        $getkan = implode($_POST['kanaler'], ' ');
        $kan = $row['kanaler'];
        $match_count = count(TheMatch($kan, $getkan));
        $match = ' Match = '.$match_count;
        }

        // I WANT THIS ECHO TO BE SORT BY THE VALUE OF '$match_count' HIGEST NR IN TOP //
        echo'<tr>
            <td style="background-color:'.$farve.'"><p>
            ' . $row['udbyder'] . ' '.$_POST['kanaler'].'
            </p></td>
    </tr>';
    }
}
4

2 回答 2

1

您必须在遍历数组时缓冲输出/值,对其进行排序然后打印。像这样的东西:

$results = array();
while($row = ...) {
    ...
    $results[] = array($match_count, '<tr><td style="background-color:...</tr>');
}

uasort($results, my_comp);

foreach($results as $result)
    echo $result[1];

这将对数组进行排序,然后根据此排序函数打印它:

function my_comp($left, $right) {
    return $left[0] > $right[0]; // to be honest I'm not 100% sure right now whether you'd need < or > for the right sorting order
}
于 2012-12-05T21:33:04.493 回答
0

您也可以使用 array_multisort :

<?php 
array_multisort($match_count, SORT_DESC, $array_values_to_print);

foreach ($match_count as $result) {
    echo '<tr><td style="background-color:...</tr>';
}
?>

第一个数组确定索引。

于 2012-12-05T22:51:39.483 回答