我试图在函数内添加foreach时出现php语法错误

时间:2014-10-18 16:36:32

标签: php codeigniter syntax foreach

我正在尝试将foreach函数用于其他函数来写文件

这是foreach函数

foreach ($from as $ex) { 
                echo "$ex->id ;";
                echo "$ex->nickname ;";
                echo "$ex->date ;";
                echo "$ex->content ;";
                echo "\n";
                }

这是codeigniter写文件函数,我想将我的foreach添加到最后一个参数

write_file('reports/report-' . date('h-i-s-d-m-y') . '.csv',$i_want_to_put_my_foreach_here);

1 个答案:

答案 0 :(得分:0)

你不能把foreach循环作为这样的函数参数。你需要将它们分开。在这种情况下,您可能希望使用临时变量:

foreach ($from as $ex) { 
    $string = "$ex->id ;";
    $string .=  "$ex->nickname ;";
    $string .=  "$ex->date ;";
    $string .=  "$ex->content ;";
    $string .=  "\n";
    write_file('reports/report-' . date('h-i-s-d-m-y') . '.csv', $string);
}