4

我在 PHP 中有这个数组,其中我在数组中有一个数组,其中包含名称和排名。我希望排名按顺序排列。即从1到16。

$info = array(  array( "Name"=> "Ayush Mishra", "Rank"=> "1"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "16"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "14"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "15"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "13"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "12"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "11"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "10"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "8"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "9"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "7"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "5"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "4"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "3"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "6"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "2"),   ); 

我想要上面数组中的这个数组。这是我想要得到的结果数组。

$result = array( 


  array( "Name"=> "Ayush Mishra", "Rank"=> "1"),  
  array( "Name"=> "Teaser", "Rank"=> "2"), 
  array( "Name"=> "Pussel", "Rank"=> "3"),
  array( "Name"=> "Condomso", "Rank"=> "4"),  
  array( "Name"=> "Branite", "Rank"=> "5"),  
  array( "Name"=> "Biknol", "Rank"=> "6"),  
  array( "Name"=> "Fuckita", "Rank"=> "7"),
  array( "Name"=> "James", "Rank"=> "8"),  
  array( "Name"=> "Babita", "Rank"=> "9"),  
  array( "Name"=> "Mathse", "Rank"=> "10"), 
  array( "Name"=> "rohan", "Rank"=> "11"),  
  array( "Name"=> "Alex", "Rank"=> "12"), 
  array( "Name"=> "Peter", "Rank"=> "13"),  
  array( "Name"=> "Gabbar Singh", "Rank"=> "14"),  
  array( "Name"=> "raghav", "Rank"=> "15"), 
  array( "Name"=> "Shivam", "Rank"=> "16"),  


             ); 

我想要上面的数组作为 $info 的结果。请给我代码以使 $info 更改为 $result。

4

7 回答 7

4

不需要更多的东西:

function sort_by_rank($a, $b) {
    return $a['Rank'] > $b['Rank'];
}

usort($info, "sort_by_rank");
print_r($info);

输出

Array
(
    [0] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 1
        )

    [1] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 2
        )

    [2] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 3
        )

    [3] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 4
        )

    [4] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 5
        )

    [5] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 6
        )

    [6] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 7
        )

    [7] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 8
        )

    [8] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 9
        )

    [9] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 10
        )

    [10] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 11
        )

    [11] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 12
        )

    [12] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 13
        )

    [13] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 14
        )

    [14] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 15
        )

    [15] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 16
        )

)

如果您想维护原始索引,从您的问题来看,这似乎不是必需的,但如果是这样,请使用uasort而不是usort.

于 2012-12-19T06:56:53.190 回答
1
// this function will sort your multidimensional array by value
function aasort (&$array, $key) {
    $sorter=array();
    $ret=array();
    reset($array);
    foreach ($array as $ii => $va) {
        $sorter[$ii]=$va[$key];
    }
    asort($sorter);
    foreach ($sorter as $ii => $va) {
        $ret[$ii]=$array[$ii];
    }
    $array=$ret;
}

$info = array(  array( "Name"=> "Ayush Mishra", "Rank"=> "1"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "16"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "14"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "15"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "13"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "12"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "11"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "10"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "8"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "9"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "7"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "5"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "4"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "3"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "6"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "2"),   ); 


aasort($info,"Rank");

echo '<pre>';print_r($info);

输出:

Array
(
    [0] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 1
        )

    [15] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 2
        )

    [13] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 3
        )

    [12] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 4
        )

    [11] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 5
        )

    [14] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 6
        )

    [10] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 7
        )

    [8] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 8
        )

    [9] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 9
        )

    [7] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 10
        )

    [6] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 11
        )

    [5] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 12
        )

    [4] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 13
        )

    [2] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 14
        )

    [3] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 15
        )

    [1] => Array
        (
            [Name] => Ayush Mishra
            [Rank] => 16
        )

)
于 2012-12-19T07:01:18.497 回答
1

使用usort,使用用户定义的比较函数按值对数组进行排序

usort($info, 'sortfn');

function sortfn($a, $b)
{
    if($a['Rank'] == $b['Rank'])
        return ($a['Rank'] > $b['Rank']);
    else
        return ($a['Rank'] < $b['Rank']);
}
于 2012-12-19T06:55:40.073 回答
1
<?php


$info = array(  array( "Name"=> "Ayush Mishra", "Rank"=> "1"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "16"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "14"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "15"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "13"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "12"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "11"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "10"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "8"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "9"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "7"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "5"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "4"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "3"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "6"),  
  array( "Name"=> "Ayush Mishra", "Rank"=> "2"),   ); 



foreach ($info as $key => $row) {
    $rank[$key]  = $row[rank];
    $name[$key] = $row['name'];
}
array_multisort($rank, SORT_DESC, $name, SORT_ASC, $info);
print_r($info);

?>
于 2012-12-19T06:56:32.197 回答
1

您可以定义自己的排序函数:

function cmp($a, $b)
{
  if ($a['Rank'] == $b['Rank'])
    return 0;
  return ($a['Rank'] < $b['Rank']) ? -1 : 1;
}

uasort($info, "cmp");

请注意,我使用uasortnot usort,这是因为维护密钥。检查文档以获取更多信息。

于 2012-12-19T06:58:13.560 回答
0

我猜你想根据排名来订购它。

这是代码,

$result = $info;

for ($i = 0; $i < count($result); $i++)
{
    for ($j = 0; $j < $i; $j++)
    {
        //sorting based on rank
        if($result[$i]["Rank"] < $result[$j]["Rank"])
        {
            //Swapping rank
            $rank = $result[$i]["Rank"];
            $result[$i]["Rank"] = $result[$j]["Rank"];
            $result[$j]["Rank"] =$rank;

            //Swapping corresponding Names
            $name = $result[$i]["Name"];
            $result[$i]["Name"] = $result[$j]["Name"];
            $result[$j]["Name"] =$name;
        }       
    }
}
于 2012-12-19T09:28:20.453 回答
-1
$rank = array();
foreach ($result as $key => $row)
{
    $rank[$key] = $row['rank'];
}
array_multisort($rank, SORT_DESC, $result);
于 2012-12-19T06:59:05.947 回答