最高成为数组中的第一位(array_search)

问题描述 投票:0回答:0

我想使用数组索引显示位置,因为我按降序排序。数组索引和排序工作正常,但问题是数组看起来像帕斯卡三角形。

<?php 
$obtained_mark_query =  $this->db->get_where('mark' , array 'class_id' => $class_id , 
                        'exam_id' => $exam_id , 
                        'subject_id' => $row2['subject_id'] , 
                        'student_id' => $row['student_id'],
                        'year' => $running_year
                        ));
if ( $obtained_mark_query->num_rows() > 0) {
    $obtained_marks = $obtained_mark_query->row()->mark_obtained;
    $ca = $obtained_mark_query->row()->ca;
    $hanus45 = $obtained_marks+$ca;
    echo $hanus45;
    if ($obtained_marks >= 0 && $obtained_marks != '') {
        $grade = $this->crud_model->get_grade($obtained_marks+$ca);
        $total_grade_point += $grade['grade_point'];
    }
    $total_marks += ($obtained_marks+$ca);
}
if($hanus45>0){
    $allsub+=1;
}
?>
</td>
<?php endforeach;?>
<td style="text-align: center;">
<?php
$this->db->where('class_id' , $class_id);
$this->db->where('year' , $running_year);
$this->db->from('subject');
$number_of_subjects = $this->db->count_all_results();
$points=($total_marks / $number_of_subjects);
echo $total_marks;?></td>
<td style="text-align: center;"><?php
$this->db->where('class_id' , $class_id);
$this->db->where('year' , $running_year);
$this->db->from('subject');
$num_subjects = $this->db->count_all_results();
$avg +=round($total_marks / $allsub,2);
echo $avg;?></td>
<td style="text-align: center;color: red">
<?php
$posi=0; 
$totalstd=$this->db->get_where('enroll' , array('class_id' => $class_id , 'year' => $running_year))->num_rows();
$duplicate = array();           
foreach($obtained_mark_query as $row): 
    if (in_array($obtained_mark_query, $duplicate))
        continue; //if we already have that id, skip it
    $duplicate[] = $obtained_mark_query;
    $push.= $avg.",";
endforeach;                
$pu=explode(",", $push);
rsort($pu);
$posi=array_search($avg, $pu)+1;
if($posi==1){
    $posi.="<sup>st</sup>";
}elseif($posi==2){
    $posi.="<sup>nd</sup>";
}elseif($posi==3){
    $posi.="<sup>rd</sup>";
}else{
    $posi.="<sup>th</sup>";
}
echo $posi;
$this->db->query("update mark set position='$posi' where class_id='$class_id' and exam_id='$exam_id' and subject_id='".$row2['subject_id']."' and student_id='".$row['student_id']."' and year='$running_year'");
?>

位置应该是降序排列的数组索引(数组中最高的是第一个索引,然后是其他索引)。

php arrays sorting codeigniter multidimensional-array
© www.soinside.com 2019 - 2024. All rights reserved.