0

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

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

$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 对这个列表进行排序的方法,那将对我很有帮助。

谢谢和干杯

判断

4

1 回答 1

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>";
}
于 2013-09-12T19:48:11.223 回答