douju1968 2013-12-02 11:40
浏览 29
已采纳

除非最终结果,否则在foreach期间回显文本

I currently have my code to output data from a database but I am unsure of how to append text during a foreach where the result is not the final entry.

//Print each result 
foreach($new_array as $array){

//print out a result with formatting  
Echo $Content1 = <<<CONTENT1
{"
CONTENT1;

Echo $array['f1'];

Echo $Content2 = <<<CONTENT2
"}{"
CONTENT2;

Echo $array['f2']; 

Echo ".";

Echo $array['f3'];

Echo $Content3 = <<<CONTENT3
"}
CONTENT3;

Echo ", " //If not final entry

};

?> 

So in short transforming the output from;

{"Entry"}{"Name.ID"}{"Entry"}{"Name.ID"}

to;

{"Entry"}{"Name.ID"}, {"Entry"}{"Name.ID"}

  • 写回答

3条回答 默认 最新

  • doucai6663 2013-12-02 11:45
    关注

    Try this

    $endvar=end($new_array);
    

    it will store the last value in the array and then you can check it using if condition

    foreach($new_array as $array){
    if($array==$endvar)
    {
    // do whatever you want to do here
    }
    else
    {
    // else code
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?