返回基于整数的数组键


Returning An Array Key Based On A Integer

我正在为每个用户存储一个分数。每个分数都应映射到一个等级。例如,得分为 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))];
}

这完全符合您的要求;)

$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)

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

我已经 10 多年没有写过 php了,但是像这样:

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

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

$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'];
    }
}

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

2)你需要循环你的键而不是数组内容,所以先获取键

$keys = array_keys($ranks)

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

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

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

试试这样的事情

<?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 )
$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

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

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