无法修改数组密钥

时间:2016-05-02 19:02:41

标签: php arrays codeigniter

数组键始终输出' 1'当我执行array_search而不管值的位置时,请问我该如何纠正? 例如' 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  }}?>

1 个答案:

答案 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  }}?>