CodeIgniter Forums
How to fix function to return multi array ci 3 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to fix function to return multi array ci 3 (/showthread.php?tid=67146)



How to fix function to return multi array ci 3 - Khanh Minh - 01-21-2017

i test with function below

PHP Code:
public function getexam_result($bode)
    {
        foreach(
$bode as $v)
 
       {
         
 $this->db->where('question_id',$v);
         
 $q=$this->db->get('answer_true');
         
 $rs=$q->result();
 
         $ans=array();
 
         foreach($rs as $v1)
 
         {
 
           $ans[]=$v1->answer_id;
 
         }
 
           return $ans;
 
       }
 
  
if i return  it only one array
PHP Code:
Array
(
 
   [0] => 1
    
[1] => 2


when i print array $ans it show array
PHP Code:
Array
(
 
   [0] => 1
    
[1] => 2
)
Array
(
 
   [0] => 2
)
Array
(
 
   [0] => 2
)
Array
(
 
   [0] => 3
)
Array
(
 
   [0] => 1
    
[1] => 3
)
Array
(
 
   [0] => 1
    
[1] => 2
)
Array
(
 
   [0] => 2
)
Array
(
 
   [0] => 1
)
Array
(
 
   [0] => 1
    
[1] => 2
)
Array
(
 
   [0] => 2
)
Array
(
 
   [0] => 2
)
Array
(
 
   [0] => 1
)
Array
(
 
   [0] => 1
)
Array
(
 
   [0] => 3
)
Array
(
 
   [0] => 3
)
Array
(
 
   [0] => 2
)
Array
(
 
   [0] => 3
)
Array
(
 
   [0] => 3
)
Array
(
 
   [0] => 1
    
[1] => 2
)
Array
(
 
   [0] => 2
)
<
pre

how to fix to return multi array
thank you in advance


RE: How to fix function to return multi array ci 3 - pdthinh - 01-21-2017

You should init $ans array before the first (outer) foreach
PHP Code:
public function getexam_result($bode)
 
   {
 
       $ans=array();
 
       foreach($bode as $v)
 
       {
 
         $this->db->where('question_id',$v);
 
         $q=$this->db->get('answer_true');
 
         $rs=$q->result();
 
         //...
        
}
        return 
$ans;
    } 



RE: How to fix function to return multi array ci 3 - Khanh Minh - 01-22-2017

tks pro #pdthinh