Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP中数组值的排名/位置_Php_Arrays - Fatal编程技术网

PHP中数组值的排名/位置

PHP中数组值的排名/位置,php,arrays,Php,Arrays,我有一个包含这些值的数组,数组[0]=>12[1]=>17[2]=>5[3]=>27[4]=>5,我需要找出每个值的排名/位置 即 这些需要和数组的顺序相同,所以输出应该如下 12 - Rank 3 17 - Rank 2 5 - Rank 4 27 - Rank 1 5 - Rank 4 您可能可以按值对数组进行排序: $ranked = sort($arrayValues); 在此之后,您可以执行for循环,在$ranked数组中循环,然后按顺序打印数组: for($i = 0

我有一个包含这些值的数组,数组[0]=>12[1]=>17[2]=>5[3]=>27[4]=>5,我需要找出每个值的排名/位置 即

这些需要和数组的顺序相同,所以输出应该如下

12 - Rank 3 
17 - Rank 2 
5 - Rank 4 
27 - Rank 1 
5 - Rank 4 

您可能可以按值对数组进行排序:

$ranked = sort($arrayValues);
在此之后,您可以执行for循环,在$ranked数组中循环,然后按顺序打印数组:

for($i = 0; $i < length($ranked); $i++){
    for($j = 0; $j < length($arrayValues); $j++){
        if($arrayValues[$i] == $sorted[$j]){
            echo $arrayValues[$i] . ' has rank ' . $j;
         }
    }
}

您可能可以按值对数组进行排序:

$ranked = sort($arrayValues);
在此之后,您可以执行for循环,在$ranked数组中循环,然后按顺序打印数组:

for($i = 0; $i < length($ranked); $i++){
    for($j = 0; $j < length($arrayValues); $j++){
        if($arrayValues[$i] == $sorted[$j]){
            echo $arrayValues[$i] . ' has rank ' . $j;
         }
    }
}

您遇到的一个问题是,您需要的返回值是相同值的2倍,并且键5=>rank4。因此,不能将列组作为键或值作为键来执行数组。在这个例子中,我只是制作了一个二维数组

<?php

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

// your original input
$original = array(12, 17, 5, 27, 5);

// strip duplicates as they are ranked equally
$rankings = array_unique($original);

// apply some sorting so that the ranks are now given by the keys.
rsort($rankings);

// now just use the origincal array and lookup the rankings for each value
$return = array();
foreach($original as $value)
{
    $rankedValue = array();
    $rankedValue['value'] = $value;
    $rankedValue['rank'] = array_search($value, $rankings) + 1;
    $return[] = $rankedValue;
}

var_dump($return);

您遇到的一个问题是,您需要的返回值是相同值的2倍,并且键5=>rank4。因此,不能将列组作为键或值作为键来执行数组。在这个例子中,我只是制作了一个二维数组

<?php

error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', 1);

// your original input
$original = array(12, 17, 5, 27, 5);

// strip duplicates as they are ranked equally
$rankings = array_unique($original);

// apply some sorting so that the ranks are now given by the keys.
rsort($rankings);

// now just use the origincal array and lookup the rankings for each value
$return = array();
foreach($original as $value)
{
    $rankedValue = array();
    $rankedValue['value'] = $value;
    $rankedValue['rank'] = array_search($value, $rankings) + 1;
    $return[] = $rankedValue;
}

var_dump($return);

这应适用于具有相同等级的重复值:

$values = array();
$values[0] = 5;
$values[1] = 12;
$values[2] = 19;
$values[3] = 9;
$values[4] = 5;

$ordered_values = $values;
rsort($ordered_values);

foreach ($values as $key => $value) {
    foreach ($ordered_values as $ordered_key => $ordered_value) {
        if ($value === $ordered_value) {
            $key = $ordered_key;
            break;
        }
    }
    echo $value . '- Rank: ' . ((int) $key + 1) . '<br/>';
}

这应适用于具有相同等级的重复值:

$values = array();
$values[0] = 5;
$values[1] = 12;
$values[2] = 19;
$values[3] = 9;
$values[4] = 5;

$ordered_values = $values;
rsort($ordered_values);

foreach ($values as $key => $value) {
    foreach ($ordered_values as $ordered_key => $ordered_value) {
        if ($value === $ordered_value) {
            $key = $ordered_key;
            break;
        }
    }
    echo $value . '- Rank: ' . ((int) $key + 1) . '<br/>';
}
试试这个

$array = Array ( "0" => 12 , "1" => 17 , "2" => 5, "3" => 27, "4" => 5 );

$i=1;
foreach($array as $key=>$values)
{
    $max = max($array);
    echo "\n".$max." rank is ". $i."\n";
    $keys = array_search($max, $array);    
    unset($array[$keys]);
    if(sizeof($array) >0)
    if(!in_array($max,$array))
        $i++;

}
试试这个

$array = Array ( "0" => 12 , "1" => 17 , "2" => 5, "3" => 27, "4" => 5 );

$i=1;
foreach($array as $key=>$values)
{
    $max = max($array);
    echo "\n".$max." rank is ". $i."\n";
    $keys = array_search($max, $array);    
    unset($array[$keys]);
    if(sizeof($array) >0)
    if(!in_array($max,$array))
        $i++;

}

只是在为自己寻找一个好的解决方案时遇到了这个话题。为了将来,把这个放在这里。我使用的是以下函数:

/* Assign rank to each value of the array $in. 
 * Args:
 *   in (array): Array containing as set of numeric values.
 *
 * Returns:
 *   Returns an array of the same length with ranks. Highest
 *   values of $in get rank 1, lower values get higher ranks.
 *   The same values are attributed to the same ranks.
 */
function array_rank( $in ) {
    # Keep input array "x" and replace values with rank.
    # This preserves the order. Working on a copy called $x
    # to set the ranks.
    print "input\n";
    print_r($in);
    $x = $in; arsort($x); 
    print "sorted\n";
    print_r($x);
    # Initival values
     $rank       = 0; 
    $hiddenrank = 0;
    $hold = null;
    foreach ( $x as $key=>$val ) {
        # Always increade hidden rank
        $hiddenrank += 1;
        # If current value is lower than previous:
        # set new hold, and set rank to hiddenrank.
        if ( is_null($hold) || $val < $hold ) {
            $rank = $hiddenrank; $hold = $val;
        }    
        # Set rank $rank for $in[$key]
        $in[$key] = $rank;
    }  
    print "ranking result\n";
    print_r($in);
    return $in; 
}
$a = array(140,180,180,100);
array_rank( $a ); 

如果你想有一个反向的排名值越低越好,简单地用asort替换rasort。就我而言,我的排名分数越高越好。阵列140180100获得等级3,1,1,4。在这种情况下,得分相同的玩家获得相同的排名,排名1,而排名2则被忽略。

只是在为自己寻找一个好的解决方案时遇到了这个话题。为了将来,把这个放在这里。我使用的是以下函数:

/* Assign rank to each value of the array $in. 
 * Args:
 *   in (array): Array containing as set of numeric values.
 *
 * Returns:
 *   Returns an array of the same length with ranks. Highest
 *   values of $in get rank 1, lower values get higher ranks.
 *   The same values are attributed to the same ranks.
 */
function array_rank( $in ) {
    # Keep input array "x" and replace values with rank.
    # This preserves the order. Working on a copy called $x
    # to set the ranks.
    print "input\n";
    print_r($in);
    $x = $in; arsort($x); 
    print "sorted\n";
    print_r($x);
    # Initival values
     $rank       = 0; 
    $hiddenrank = 0;
    $hold = null;
    foreach ( $x as $key=>$val ) {
        # Always increade hidden rank
        $hiddenrank += 1;
        # If current value is lower than previous:
        # set new hold, and set rank to hiddenrank.
        if ( is_null($hold) || $val < $hold ) {
            $rank = $hiddenrank; $hold = $val;
        }    
        # Set rank $rank for $in[$key]
        $in[$key] = $rank;
    }  
    print "ranking result\n";
    print_r($in);
    return $in; 
}
$a = array(140,180,180,100);
array_rank( $a ); 

如果你想有一个反向的排名值越低越好,简单地用asort替换rasort。就我而言,我的排名分数越高越好。阵列140180100获得等级3,1,1,4。得分相同的玩家获得相同的排名,在本例中排名1,而排名2则被忽略。

搜索排序功能Show是27排名2?我不明白?您只知道27具有数组键3,因此位于数组中的第四个位置。Andresch。。我在输出中给27分排名1,没有人会做你的家庭作业。不是因为很难,而是因为你没有尝试。您需要填充另一个数组,以便用列组填充值。使用相同的键不更改顺序。搜索排序函数Show是27秩2?我不明白?您只知道27具有数组键3,因此位于数组中的第四个位置。Andresch。。我在输出中给27分排名1,没有人会做你的家庭作业。不是因为很难,而是因为你没有尝试。您需要填充另一个数组,以便用列组填充值。使用相同的键不更改顺序。