• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to fix function to return multi array ci 3

#1
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
Reply

#2
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;
    } 
Reply

#3
tks pro #pdthinh
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.