2016-05-02 91 views
0

无论数值的位置,我做array_search时,数组键总是输出'1',请问我该如何纠正? 的“174”敌人例如array_search应该输出4为重点无法获取阵列键

Array search of 174 print_r

<?php 

$sid = $this->db->get_where('student' , array('class_id' => $class_id))->result_array(); 

/** Get Mark of Each Student **/ 

foreach($sid as $rowm){ 
    $class_pos = $this->crud_model->get_exam_total($row2['exam_id'] , $class_id , $rowm['student_id']); 

    foreach($class_pos as $keys => $class_posi){ 

     $arr = $class_posi['mark_obtained']; 
     $arra = array($keys + 1=> $arr); 
     $resulte = array_search(174, $arra); 

     ?> 
     <td style="text-align: center;"><?php echo $resulte; ?></td> 
<?php }}?> 
+0

您还可以接受的答案,如果那种出了问题,对未来用户很有用 –

回答

2

试试这个

<?php 

$sid = $this->db->get_where('student' , array('class_id' => $class_id))->result_array(); 

/** Get Mark of Each Student **/ 
$arra = array(); 

$arra[]=-1;// this will start index searching from 1 

foreach($sid as $rowm){ 


$class_pos = $this->crud_model->get_exam_total($row2['exam_id'] , $class_id , $rowm['student_id']); 


foreach($class_pos as $keys => $class_posi){ 

$arr = $class_posi['mark_obtained']; 
$arra[] = $arr; 
$resulte = array_search(174, $arra); 

?> 
<td style="text-align: center;"><?php echo $resulte; ?></td> 
<?php }}?> 
+0

输出结果为zer0 – 4Jean

+0

@ 4Jean啊右键进行了编辑,你能回显$ arra以确保你有174分的标记是由学生获得的吗? –

+0

当我回声$ arra它展示所有字段上的数组关键字'数组' – 4Jean