0

I want to assign or store the values I get from foreach loop into an array, so that I can use array sort function for the values. Please below is my code. It seems to create a whole new array for every loop with key = 0. And if i place the print statement below the loop, it will display only the last value obtained from the loop. I don't understand how it was explained in This Question

<?php  

  $studen_id = $this->db->get_where('student' , array('class_id' => $class_id))->result_array();
    foreach($studen_id as $row){
    $mark_obtained = $this->crud_model->get_exam_total($row2['exam_id'] , $class_id , $row['student_id']);

        $student_mark = array($mark_obtained);

        // rsort($student_mark);

        echo "<li>";  print_r($student_mark);  echo "</li>";

    }
?>

Output ARRAY

5 Answers 5

4

You need to add [] to generate proper array. Try this:

<?php  
  $studen_id = $this->db->get_where('student' , array('class_id' => $class_id))->result_array();
  $student_mark = array();
  foreach($studen_id as $row){
    $mark_obtained = $this->crud_model->get_exam_total($row2['exam_id'] , $class_id , $row['student_id']);
    $student_mark[] = $mark_obtained;
  }
?>
Sign up to request clarification or add additional context in comments.

2 Comments

3 correct answers but "Fastest gun in the west" gets my vote :-)
Also, per OP's question, I recommend showing an example of using a sort function after the foreach() loop.
3

Create an empty array outside the loop:

$Array = array();

Within the loop add the values:

$Array[] = $mark_obtained;

After the end of loop test your array:

print_r($Array);

1 Comment

Thanks guys for all the answers it worked. Im grateful
1

To create only one array, use this form:

$student_mark = array();
foreach($studen_id as $row){
 $mark_obtained = ......
  $student_mark[] = $mark_obtained;
  ....

Comments

1

Try to use array_push(). example:

<?php
    $student_mark = array();
    $studen_id = $this->db->get_where('student' , array('class_id' => $class_id))->result_array();

    foreach($studen_id as $row){
        $mark_obtained = $this->crud_model->get_exam_total($row2['exam_id'] , $class_id , $row['student_id']);

        array_push($student_mark, $mark_obtained);

        // rsort($student_mark);

        echo "<li>";  print_r($student_mark);  echo "</li>";
    }
?>

$student_marks is now a flat array...

Regards.

3 Comments

Ahem, shouldn't it be array_push($student_mark, $mark_obtained); ?
Sorry, I edit my post in order to use array_merge... You talk about the array() function, yeah copy/paste error ;).
$mark_obtained is not an array so array_merge() is going to produce an error...
1

I think this is what you want ...

$student_mark =array();
$studen_id = $this->db->get_where('student' , array('class_id' => $class_id))->result_array();
foreach($studen_id as $row)
{
    $mark_obtained = $this->crud_model->get_exam_total($row2['exam_id'] , $class_id , $row['student_id']);
    $student_mark[] = $mark_obtained;        
}

sort($student_mark);
$arrlength = count($student_mark);

for($x = 0; $x < $arrlength; $x++) 
{
    echo "<li>".$student_mark[$x]."</li>";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.