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

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");

キーを維持しているため、使用しuasortなかったことに注意してください。詳細については、ドキュメントusortを確認してください。

于 2012-12-19T06:58:13.560 に答える
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 に答える
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 に答える