Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.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 - Fatal编程技术网

如何在php中对数组进行降序和升序排序?

如何在php中对数组进行降序和升序排序?,php,Php,我有一个由GPA和学生年龄组成的数组,都在一个学生班级中。我必须按降序排列GPA,然后按升序排列年龄,以防两个学生的GPA相同 这是学生班: class Student { private $gpa; private $age; public function __construct($gpa, $age) { $this->gpa = $gpa; $this->age = $age; } public

我有一个由GPA和学生年龄组成的数组,都在一个学生班级中。我必须按降序排列GPA,然后按升序排列年龄,以防两个学生的GPA相同

这是学生班:

class Student
{
    private $gpa;
    private $age;
    public function __construct($gpa, $age)
    {
        $this->gpa = $gpa;
        $this->age = $age;
    }
    public function getGPA()
    {
        return $this->gpa;
    }
    public function getAge()
    {
        return $this->age;
    }
}
到目前为止,我尝试在学生的课堂上创建两个函数:

public function gpaRange() {
        return rsort($gpa);
}

public function ageRage() {
        return sort($age);
}

这是我的解决方案,它利用了冒泡排序,首先对GPA进行降序,然后在找到多个GPA的组之后,对年龄进行升序。警告:这是一个略长的解决方案,有点难看(各位,请随意简化我的答案)

我还包括了一个更大的阵列来测试它

class Student
{
    private $gpa;
    private $age;
    public function __construct($gpa, $age)
    {
        $this->gpa = $gpa;
        $this->age = $age;
    }
    public function getGPA()
    {
        return $this->gpa;
    }
    public function getAge()
    {
        return $this->age;
    }
}


$students = 
    array(new Student(4.0,22),
          new Student(3.3,21),
          new Student(2.7,22),
          new Student(3.4,19),
          new Student(3.6,22),
          new Student(4.0,20),
          new Student(3.0,21),
          new Student(3.0,19),
          new Student(3.0,20));

foreach ($students as $a)
{
    echo $a->getGPA().", ".$a->getAge()."<br>";
}

echo "<br>";


//First, sort by GPA descending using bubble sort.
$sorted = false;
while ($sorted == false)
{
    $sorted = true;
    for ($x = 0; $x < sizeof($students) - 1; $x++)
    {

        //Clear temporary array.
        $temp_array = array();

        //Compare the current entry and the one right after it.
        //If in the wrong, order populate the temporary array to re-enter the values in the main array.
        if ($students[$x]->getGPA() < $students[$x+1]->getGPA())
        {
            $temp_array[0] = $students[$x+1];
            $temp_array[1] = $students[$x];

            $students[$x] = $temp_array[0];
            $students[$x+1] = $temp_array[1];
            $sorted = false;
        }
    }

    //The loop continues until all GPA entries are sorted. 
}



//Secondly, sort by age.
$counter = 0;
$target_gpa = 0;
$indices = array();
while ($counter < sizeof($students))
{
    if ($target_gpa == 0)
    {
        $target_gpa = $students[$counter]->getGPA();
        array_push($indices, $counter);
    }
    else 
    {
        if ($students[$counter]->getGPA() == $target_gpa)
        {
            array_push($indices, $counter);
        }
        else if ($students[$counter]->getGPA() != $target_gpa and sizeof($indices) < 2)
        {
            $indices = array();
            $target_gpa = $students[$counter]->getGPA();
            array_push($indices, $counter);
        }
        else if ($students[$counter]->getGPA() != $target_gpa and sizeof($indices) > 1 or $students[$counter]->getGPA() == $target_gpa and $counter + 1 == sizeof($students)- 1)
        {
            if ($counter + 1 == sizeof($students) - 1)
            {
                array_push($indices, $counter + 1);
            }

            //Bubble sort.
            $sorted = false;
            while ($sorted == false)
            {
                $sorted = true;
                for ($x = 0; $x < sizeof($indices) - 1; $x++)
                {

                    //Clear temporary array.
                    //$temp_array = array();

                    //Compare the current entry and the one right after it.
                    //If in the wrong order, populate the temporary array to re-enter the values in the main array.

                    if ($students[$indices[$x]]->getAge() > $students[$indices[$x+1]]->getAge())
                    {
                        //echo "Temp arrays: ".$temp_array[0]->getAge().", ".$temp_array[1]->getAge()."<br>";
                        $temp_array[0] = $students[$indices[$x+1]];
                        $temp_array[1] = $students[$indices[$x]];

                        $students[$indices[$x]] = $temp_array[0];
                        $students[$indices[$x+1]] = $temp_array[1];
                        $sorted = false;
                    }
                }

            }

            //Replace the indices array with the new entry.
            $indices = array();
            $target_gpa = $students[$counter]->getGPA();
            array_push($indices, $counter);

        }


    }

    $counter += 1;
}

foreach ($students as $a)
{
    echo $a->getGPA().", ".$a->getAge()."<br>";
}

班级学生
{
私人机构(gpa元);;
私人年龄;
公共功能构造($gpa,$age)
{
$this->gpa=$gpa;
$this->age=$age;
}
公共函数getGPA()
{
返回$this->gpa;
}
公共函数getAge()
{
返回$this->age;
}
}
$学生=
阵列(新生(4.0,22),
新生(3.3,21),
新生(2.7,22),
新生(3.4,19),
新生(3.6,22),
新生(4.0,20),
新生(3.0,21),
新生(3.0,19),
新生(3.0,20));
foreach(学生为$a)
{
echo$a->getGPA()。”,“$a->getAge()。”
”; } 回声“
”; //首先,使用冒泡排序按GPA降序排序。 $sorted=false; 而($sorted==false) { $sorted=true; 对于($x=0;$xgetGPA()<$students[$x+1]->getGPA()) { $temp_数组[0]=$students[$x+1]; $temp_数组[1]=$students[$x]; $students[$x]=$temp_数组[0]; $students[$x+1]=$temp_数组[1]; $sorted=false; } } //循环将继续,直到对所有GPA条目进行排序。 } //第二,按年龄分类。 $counter=0; $target_gpa=0; $index=array(); 而($countergetGPA(); 数组_push($index,$counter); } 其他的 { 如果($students[$counter]->getGPA()==$target\u gpa) { 数组_push($index,$counter); } else if($students[$counter]->getGPA()!=$target\u gpa和sizeof($index)<2) { $index=array(); $target_gpa=$students[$counter]->getGPA(); 数组_push($index,$counter); } 如果($students[$counter]->getGPA()!=$target\u gpa和sizeof($index)>1或$students[$counter]->getGPA()==$target\u gpa和$counter+1==sizeof($students)-1) { 如果($counter+1==sizeof($students)-1) { 数组推送($index,$counter+1); } //气泡排序。 $sorted=false; 而($sorted==false) { $sorted=true; 对于($x=0;$xgetAge()>$students[$index[$x+1]]]->getAge()) { //回显“临时数组:“..Temp_数组[0]->getAge()”,“$Temp_数组[1]->getAge()”
”; $temp_数组[0]=$students[$index[$x+1]]; $temp_数组[1]=$students[$index[$x]]; $students[$index[$x]]=$temp_数组[0]; $students[$index[$x+1]]=$temp_数组[1]; $sorted=false; } } } //用新条目替换索引数组。 $index=array(); $target_gpa=$students[$counter]->getGPA(); 数组_push($index,$counter); } } $counter+=1; } foreach(学生为$a) { echo$a->getGPA()。”,“$a->getAge()。”
”; }
您可以使用usort()及其自己的比较函数对学生类对象数组进行排序

$sortFctGpaAge = function($a, $b){
  $cmp = $b->getGPA() <=> $a->getGPA();  //desc
  if($cmp == 0) $cmp = $a->getAge() <=> $b->getAge(); //asc 
  return $cmp;
};

usort($students,$sortFctGpaAge);
$sortFctGpaAge=函数($a,$b){
$cmp=$b->getGPA()$a->getGPA();//描述
如果($cmp==0)$cmp=$a->getAge()$b->getAge();//asc
返回$cmp;
};
usort(学生,$sortFctGpaAge);

请注意降序和升序排序比较中参数的顺序。

为了确定问题:是否有
$gpa
$age
两个数组,其中每个索引都指向同一个学生?您能否向我们展示这些数组中的一些示例值?另外,它与PHP7有什么关系?根据我对这个问题的理解,我在student中有一个student OBJ数组,即gpa和年龄。是的,每个指数都是指同一个学生,比如,学生1的平均成绩是4.0,他们是22岁,学生2,3.5岁和19岁,依此类推。同样对于php7标记,请不要介意,我一定是意外地将其与php标记一起添加的。什么问题?是你在问问题!你有所有的细节,这就是为什么我们要求你澄清。如果你有一个student对象数组,这意味着每个
student
都有一个GPA和一个年龄(都是标量),这是非常合乎逻辑的。因此,您可以使用自定义排序:
usort($Student,function(Student$a,Student$b){return$a->getGPA()$b->getGPA()| |$b->getAge()$a->getAge();})