English 中文(简体)
按职等分列的组合
原标题:Sort an array by grade
  • 时间:2012-04-25 02:44:56
  •  标签:
  • php
  • arrays
    $students = array (
     256 => array ( name  =>  Jon ,  grade  => 98.5),
     2 => array ( name  =>  Vance ,  grade  => 85.1),
     9 => array ( name  =>  Stephen ,  grade  => 94.0),
     364 => array ( name  =>  Steve ,  grade  => 85.1),
     68 => array ( name  =>  Rob ,  grade  => 74.6)
     );

 function grade_sort($x,$y){
  return ($x[ grade ] < $y[ grade ]);
  }
uasort ($students,  grade_sort );

i 是新学习者,即能很好地理解上述准则。 可修改<代码>(x[职等]及以下;y[职等]);至return$x <$y;如果没有,为什么? 感谢

最佳回答

可修改<条码>回报(x[职等]及以下;美元[职等]);至<条码>,回扣美元×及带;y;?

No. $x and $y will be one of the arrays inside $students.
E.g. $x may be array( name => Jon , grade => 98.5)
and $y may be array( name => Vance , grade => 85.1).
So return $x < $y doesn t make sense.

不过,该法典并不好。 比较功能<代码> 如果两个要素均被认为平等,如果第一个要素较低,一个数值为负数,第二个数值为正数,则该数值即为正数。 页: 1 正确的法典是:

function grade_sort($x, $y){
    return $y[ grade ] - $x[ grade ];
}
uasort($students,  grade_sort );

uasort就简单地将这一职能称为两个不同的要素,直到它知道哪些要素比什么内容大,并且已经将其全部分类。

问题回答

<代码>x和y> 是您主要<代码>$_students/code>阵列中的单体。 由于你想要按职等分类,你需要查阅<编码> 职等<>/代码>的关键。 移除会告诉它比较阵列,这显然赢得了工作。

您通过<代码> ort的比较功能将作为每一次比较的阵列要素,从而<代码>return$x <y获得一定意义和意义,因为你正在比较阵列而不是阵列中的actual Value。

First of all I would like to correct your code piece.

$students = array (
 256 => array ( name  =>  Jon ,  grade  => 98.5),
 2 => array ( name  =>  Vance ,  grade  => 85.1),
 9 => array ( name  =>  Stephen ,  grade  => 94.0),
 364 => array ( name  =>  Steve ,  grade  => 85.1),
 68 => array ( name  =>  Rob ,  grade  => 74.6)
 );  

 function grade_sort($x,$y){
    return ($x[ grade ] < $y[ grade ]);
 }
 uasort ($students, grade_sort );
 print_r($students);

现在回复到你的答复。 你不能使用<条码>回扣的x和t美元;y,因为X美元和美元是一个阵列,你想将学生的年级与你规定的职衔“级——学生”。 http://php.net/manual/en/Function.uasort.php rel=“nofollow” http://php.net/manual/en/Function.uasort.php





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签