doubianxian6557 2014-09-17 13:02
浏览 109

php函数返回foreach循环后的最后一行

The following function returns only the last row of an array:

function myFunc () {

            $sql = mySql(); 
            $stid = oci_parse(getConnect(),$sql);

// runs the query above                   
oci_execute($stid);

if (oci_execute($stid)) {
            while ($row =oci_fetch_array($stid,OCI_ASSOC+OCI_RETURN_NULLS)) {
                   $out1 = "";
                   foreach($row as $column =>$entry)
                           $out1 .= $entry;
                   $output = $out1;         
                   //var_dump($output); - here I can see all array elements                                                               
                                                                            }   
 return($output);
}                       
else return "No Oracle connection";
}

var_dump() shows all the array elements, but the function displays only the last row of the array. Is it because function's return? Do I have to return an array to get all array elements? How can I get all array elements in a one string?

  • 写回答

3条回答 默认 最新

  • dsg41888 2014-09-17 13:04
    关注

    You override $output in each loop iteration. You need to store those values in an array (or append them depending on what you ultimately want):

    $output = array();
    while ($row = oci_fetch_array($stid,OCI_ASSOC+OCI_RETURN_NULLS)) {
        $out1 = "";
        foreach($row as $column =>$entry) {
            $out1 .= $entry;                                                          
        }   
        $output[] = $out1; 
    }         
    return($output);
    

    This function is kinda convoluted and I'm pretty sure this can be greatly simplified, starting with the query.

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计