从数组中的数组创建升序数组

时间:2012-12-19 06:50:40

标签: php arrays multidimensional-array

我在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。

7 个答案:

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

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

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

?>

答案 3 :(得分:1)

您可以定义自己的排序功能:

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

uasort($info, "cmp");

请注意,我使用uasort而不是usort,这是因为维护密钥。查看documentation了解详情。

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

)

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

答案 6 :(得分:-1)

$rank = array();
foreach ($result as $key => $row)
{
    $rank[$key] = $row['rank'];
}
array_multisort($rank, SORT_DESC, $result);