0

他にどの用途が最も共通の関心を持っているかをユーザーが確認できるリストを作成したいと考えています。私はすでにすべてのクラスを作成しており、数値出力は正しいですが、リストを並べ替える方法がわからないため、共通の関心が最も高い (最も高い数値) 人がリストの一番上にあり、そうでない人はリストの一番上にあります。多くの (最も低い数字) が一番下にあります。

Web ページに値を挿入するための私のコードは次のようになります。

$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 に答える