PHP:如何在字符串中连接 foreach?

我想使用 MPDF 的库创建一个 PDF 生成器。在我在 PDF 生成器中创建的表中(当然使用 HTML),我想执行一个 foreach() 循环。这是我的代码:


$html = '<td style="border: 1px solid black; border-collapse: collapse;">

    // Here I want foreach concatenation

</td>

<td style="border: 1px solid black; border-collapse: collapse;">

    // Here I want foreach concatenation                                

</td>

<td style="border: 1px solid black; border-collapse: collapse;">

    // Here I want foreach concatenation                                

';

我尝试使用带有变量的普通技术。('.$var.'),但这没有成功。我也试过匿名函数...


            $PDFFunction = 

                function($arr) {

                    foreach($arr as $key) {

                       // And now somewhere, i've got to return the $key... But I tried in the foreach loop and outside the loop...

                    }

                };

正如您在我的评论中看到的,我不知道该将 return() 函数放在哪里。当我把它放在循环中时,它只显示第一个键,当我把它放在循环外(但在函数内)时,我只得到了最后一个键......有谁知道这个问题的解决方案?


SMILET
浏览 223回答 1
1回答

猛跑小猪

并不完全清楚您在 PDF 中“想要连接”什么,但您可以在构建文档时直接在文档中进行。下面包括您的$html变量的一部分,其中包含foreach基于您对尝试执行的操作的描述的循环。$html = '</tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td style="border: 1px solid black; border-collapse: collapse;">';foreach($arr as $key) {&nbsp; &nbsp; $html .= $key;}$html .= '</td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td style="border: 1px solid black; border-collapse: collapse;">';foreach($arr as $key) {&nbsp; &nbsp; $html .= $key;}$html .= '</td>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td style="border: 1px solid black; border-collapse: collapse;">';foreach($arr as $key) {&nbsp; &nbsp; $html .= $key;}$html .= '</td>';
打开App,查看更多内容
随时随地看视频慕课网APP