-1

我正在为我的每个用户存储一个分数。每个分数都应该映射到一个等级。例如,分数为 17 的人将被排名blogger,因为blogger其分数要求为 15。

$score = 17;

$rank = array(
    15 => array('profile_rank_name' => 'Blogger', 'profile_rank_image' => 'blogger.png'),
    18 => array('profile_rank_name' => 'News Editor', 'profile_rank_image' => 'news_editor.png'),
    23 => array('profile_rank_name' => 'Researcher', 'profile_rank_image' => 'researcher.png'),
    29 => array('profile_rank_name' => 'Publications Assistant', 'profile_rank_image' => 'publications_assistant.png'),
    36 => array('profile_rank_name' => 'Editorial Assistant', 'profile_rank_image' => 'editorial_assistant.png'),
    45 => array('profile_rank_name' => 'Copy Editor', 'profile_rank_image' => 'copy_editor.png'),
)

因为,在这种情况下,分数是 17,那么应该返回 $rank[15]。因为 $score 大于或等于 15。我该怎么做呢?

编辑:

Uksort 使用用户定义的比较函数按键对数组进行排序。我不确定它在内部是如何工作的。在下面的函数中,什么是 $a,什么是 $b?

if( ! function_exists('cmp'))
{
    function cmp($a, $b)
    {
        return $a;
    }
}

uksort($rank, "cmp");

编辑:我注意到我的问题含糊不清,我很抱歉,因为 iut 是凌晨 3 点,而且我没有像往常那样清楚地思考。谢谢大家的回复。我必须考虑重新表述这个问题。

接受的答案

public function get_profile_rank($score)
{
    /* This method exists as an optimisation effort. Ranks are defined within the database table `author_profile_rank`.
     * When we don't need application functionality on ranks and we only need to display the rank name and image we
     * call this method. It saves using a table join to retrieve the rank name and image.
     * http://stackoverflow.com/questions/19886351/returning-an-array-key-based-on-a-integer/19886467?noredirect=1#comment29583797_19886467
     */

    if($score <= 17)
    {
        return array('profile_rank_name' => 'Blogger', 'profile_rank_image' => 'blogger.png');
    }
    elseif($score >= 45)
    {
        return array('profile_rank_name' => 'Copy Editor', 'profile_rank_image' => 'copy_editor.png');
    }

    $ranks = array(
        23 => array('profile_rank_name' => 'Researcher', 'profile_rank_image' => 'researcher.png'),
        29 => array('profile_rank_name' => 'Publications Assistant', 'profile_rank_image' => 'publications_assistant.png'),
        36 => array('profile_rank_name' => 'Editorial Assistant', 'profile_rank_image' => 'editorial_assistant.png'),
    );

    $lower = function($val) use ($score)
    {
        if($val <= $score) return TRUE;
    };

    return $ranks[max(array_filter(array_keys($ranks), $lower))];
}
4

6 回答 6

2

如果值小于或等于它们的分数,则遍历您的数组并为数组中的项目设置一个变量。当值更大时中断。

我已经 10 多年没有写过 php 了,但是类似:

foreach($rank as $currentRank=>$rankData){
    if($currentRank <= $score) $matchedRank = $rankData;
    else break;
}
于 2013-11-10T04:53:44.010 回答
2

这正是你想要的;)

$score = 17;

$rank = array(
    15 => array('profile_rank_name' => 'Blogger', 'profile_rank_image' => 'blogger.png'),
    18 => array('profile_rank_name' => 'News Editor', 'profile_rank_image' => 'news_editor.png'),
    23 => array('profile_rank_name' => 'Researcher', 'profile_rank_image' => 'researcher.png'),
    29 => array('profile_rank_name' => 'Publications Assistant', 'profile_rank_image' => 'publications_assistant.png'),
    36 => array('profile_rank_name' => 'Editorial Assistant', 'profile_rank_image' => 'editorial_assistant.png'),
    45 => array('profile_rank_name' => 'Copy Editor', 'profile_rank_image' => 'copy_editor.png'),
);

$keys = array_keys($rank);

$lower = function ($val) use ($score){
    if($val <= $score) return true;
};


 $key_res = array_filter($keys, $lower);

 $user_rank = $rank[max($key_res)];

 var_dump($user_rank);

输出

array (size=2)
  'profile_rank_name' => string 'Blogger' (length=7)
  'profile_rank_image' => string 'blogger.png' (length=11)
于 2013-11-10T05:06:28.973 回答
1

可能是答案?根据您的问题:

$score = 17;

$ranks = array(
    15 => array('profile_rank_name' => 'Blogger', 'profile_rank_image' => 'blogger.png'),
    18 => array('profile_rank_name' => 'News Editor', 'profile_rank_image' => 'news_editor.png'),
    23 => array('profile_rank_name' => 'Researcher', 'profile_rank_image' => 'researcher.png'),
    29 => array('profile_rank_name' => 'Publications Assistant', 'profile_rank_image' => 'publications_assistant.png'),
    36 => array('profile_rank_name' => 'Editorial Assistant', 'profile_rank_image' => 'editorial_assistant.png'),
    45 => array('profile_rank_name' => 'Copy Editor', 'profile_rank_image' => 'copy_editor.png')
);

foreach($ranks as $rank)
{
    if($score >= $rank )
    {
        echo $rank['profile_rank_name']."-".$rank['profile_rank_image'];
    }
}
于 2013-11-10T04:44:34.910 回答
1

1)对您的数组进行排序,以便排名按递增顺序排列(如果未排序,则下一个如果将失败)

2)您需要循环您的密钥而不是数组内容,因此首先获取密钥

$keys = array_keys($ranks)

3)使用 foreach 或 for 两者中的任何一个来循环数组

foreach($keys as $key){
   if($score >= $key){
       echo $ranks[$key];
   }
}

注意:如果没有排序,当上面的代码运行时你会得到错误的结果,如果有 1000 个等级,一个有效的方法是使用二进制搜索来获得 $score 在 $ranks 中的粗略位置

于 2013-11-10T04:52:45.823 回答
1

尝试这样的事情

<?php
    $score = 17;

$rank = array(
    15 => array('profile_rank_name' => 'Blogger', 'profile_rank_image' => 'blogger.png'),
    18 => array('profile_rank_name' => 'News Editor', 'profile_rank_image' => 'news_editor.png'),
    23 => array('profile_rank_name' => 'Researcher', 'profile_rank_image' => 'researcher.png'),
    29 => array('profile_rank_name' => 'Publications Assistant', 'profile_rank_image' => 'publications_assistant.png'),
    36 => array('profile_rank_name' => 'Editorial Assistant', 'profile_rank_image' => 'editorial_assistant.png'),
    45 => array('profile_rank_name' => 'Copy Editor', 'profile_rank_image' => 'copy_editor.png'),
);


foreach ($rank as $k=>$v)
{
    if($score<=$rank[$k])
    {
        print_r($v);//Prints the first array element.
        break; 
    }
}

输出 :

Array ( [profile_rank_name] => Blogger [profile_rank_image] => blogger.png )
于 2013-11-10T04:53:00.263 回答
0
$keys = array_keys($rank);
sort($keys); // not needed in your example, just to be *bug* free
$score = 17;
$index = array_reduce($keys, function ($prev, $next) use ($score) {
    return ($prev <= $score && $score < $next) ? $prev : $next;
}) ?: min($keys);
var_dump($index); // int(15)
print $rank[$index]['profile_rank_name']; // Blogger

$index$score如果小于最小键,则将是最小数组键。所以它适用于$score < min($keys)$score > max($keys)。:)

工作演示:https ://eval.in/65190

演示:https$score : //eval.in/65196range(0, 100)

PS:PHP 5.3+(我使用的是短三元和闭包回调)

于 2013-11-10T05:00:04.933 回答