基于整数返回数组键

时间:2013-11-10 04:39:21

标签: php sorting ranking

我正在为每个用户存储分数。每个分数应映射到排名。例如,得分为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]。因为$得分大于或等于15.我怎么能这样做呢?

编辑:

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

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

uksort($ rank,“cmp”);

编辑:我注意到我的问题含糊不清,我很抱歉,因为我是凌晨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))];
}

6 个答案:

答案 0 :(得分:2)

循环遍历数组,如果值小于或等于其分数,则将变量设置为数组中的项。当值更大时中断。

我在10年多来没有写过PHP,但是有点像:

foreach($rank as $currentRank=>$rankData){
    if($currentRank <= $score) $matchedRank = $rankData;
    else break;
}

答案 1 :(得分: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)

答案 2 :(得分: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'];
    }
}

答案 3 :(得分:1)

1)对数组进行排序,使得排名按顺序递增(如果未按顺序排序,则会失败)

2)你需要循环你的键而不是数组内容所以得到键第一次

$keys = array_keys($ranks)

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

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

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

答案 4 :(得分: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 )

答案 5 :(得分: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

$scorerange(0, 100)的演示:https://eval.in/65196

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