2013-11-26 65 views
1

我有這樣的foreach循環。如何將數組值從foreach循環中移出?

if (is_object($res_two)) { 
    if($res_two->num_rows() == 0) { 
    echo "No Records Found";} 
    else if($res_two->num_rows() > 0) { 
    foreach ($res_two->result() as $row) { 
     echo $row->js_id."\t". $row->designation."\t".$row->full_name."\t".$row->location."\t".$row->graduated_in."\t";  
     $mail2 = $row->email;  
     echo $mail2 ; 

     ?> <a href="<?php echo base_url("uploads/".$row->resume."")?>">Download Resume</a> <br/><br/> <?php 
    } 
    } 
} 
?> 

現在我想提取$ mail2的詳細信息。但是,只給出$ mail2的代碼只給出一個值而不是數組(如果foreach循環迭代,它應該有多個值?)。

如何在代碼之外獲取$ mail2的多個值?

+0

只要把它們放在數組''郵件[] = $ row-> email' – Volvox

+1

試試這個。 Insted:$ mail2 = $ row-> email;使用:$ mail2 [] = $ row-> email ;.現在email2是數組,您可以在foreach循環之外獲得所有電子郵件 – Luka

+0

非常感謝。這工作得很好。 – rgk

回答

1
<?php 
if (is_object($res_two)) { 
    if ($res_two->num_rows() == 0) { 
     echo "No Records Found"; 
    } else if ($res_two->num_rows() > 0) { 
     foreach ($res_two->result() as $row) { 
      echo $row->js_id . "\t" . $row->designation . "\t" . $row->full_name . "\t" . $row->location . "\t" . $row->graduated_in . "\t"; 
      $mail2[] = $row->email; 

      ?> <a href="<?php echo base_url("uploads/" . $row->resume . "") ?>">Download Resume</a> <br/><br/> <?php 
     } 
    } 
} 
print_r($mail2); 
?> 
+0

你回答正確,但OP有錯誤的問題哈哈 –