按外部变量排序

时间:2013-09-12 19:41:04

标签: javascript php jquery html

我想创建一个列表,用户可以在其中查看哪些其他用途最常见。 我已经创建了所有类,数字输出是正确的,但我不知道如何对列表进行排序,所以最常见的兴趣(最高数字)的人在列表的顶部,而没有很多(最低的数字)在底部。

我将代码插入网页的代码如下所示:

$currentUserInterest = $interestutil->getCurrentUserInterest()->interestlist;
$otherUsersInterest = $interestutil->getAllOtherUserInterest();
foreach ($otherUsersInterest as $key => $user) 
{
    $commonInterests =count($currentUserInterest) - count(array_diff($currentUserInterest, $user->interestlist));
    echo "<li>" . $user->fname . " " . $user->lname ." $commonInterests gemeinsame Interessen</span>";
}

如果有人能用html / javascript / jquery / php告诉我一个方法来排序这个列表,那对我来说真的很有帮助。

谢谢和干杯

Jutschge

1 个答案:

答案 0 :(得分:0)

您将不得不遍历所有用户,构建一个$commonInterests数组,对该数组进行排序,然后按排序的$commonInterests数组的顺序输出用户:

$currentUserInterest = $interestutil->getCurrentUserInterest()->interestlist;
$otherUsersInterest = $interestutil->getAllOtherUserInterest();

$commonInterests = array();
foreach ($otherUsersInterest as $key => $user) {
    // You can use `array_intersect` instead of `array_diff` here.
    $commonInterests[$key] = count(array_intersect($currentUserInterest, $user->interestlist));
    //$commonInterests[$key] = count($currentUserInterest) - count(array_diff($currentUserInterest, $user->interestlist));
}

// This sort function preserves each $key, whereas `sort` would rekey the array.
// It will sort in increasing order (i.e. 1, 2, 3...). For decreasing order
// (i.e. 3, 2, 1...) use `arsort` instead.
asort($commonInterests);

foreach ($commonInterests as $key => $commonInterestsCount) {
    $user = $otherUsersInterest[$key];

    echo "<li>{$user->fname} {$user->lname} $commonInterestsCount gemeinsame Interessen</span>";
}