4Jean
4Jean

Reputation: 765

unable to inrement array key

The array key is always outputting '1' when i do the array_search no matter the position of value, please how do i correct this? Foe example array_search of '174' is supposed to output 4 as the key

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

Upvotes: 0

Views: 48

Answers (1)

Sameer Kumar Jain
Sameer Kumar Jain

Reputation: 2135

Try this

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

Upvotes: 2

Related Questions