PHP:在foreach循环中连接一个字符串 [英] PHP: Concat a string in a foreach loop
本文介绍了PHP:在foreach循环中连接一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为每个循环在a内连接一个字符串?我在下面做的事情可以在没有循环的情况下正常运行,但是有没有一种方法可以在不结束$body[{{body}}]
并重新启动的情况下将循环放入其中?
How can I concat a string inside a for each loop? What I'm doing below works ok without the loop, but is there a way I can put the loop in there without ending the $body[{{body}}]
and starting it again?
$carrier = array (
'SAS' => array('alias' => 'sas', 'name' => 'SAS'),
'British Airways' => array('alias' => 'british_airways', 'name' => 'British Airways')
);
$body['{{body}}'] = '';
$body['{{body}}'] .= 'Line one'.
foreach ($carrier as $key=>$value) {
'<option value='.$value['alias'].'>'.$value['name'].'</option>'.;
}
'Line two'.
'Line three'.
'Line four';
print_r($body);
推荐答案
尝试一下-您必须将foreach循环中生成的每一行连接起来.
Try this - you have to concatenate each line generated in the foreach loop.
$carrier = array (
'SAS' => array('alias' => 'sas', 'name' => 'SAS'),
'British Airways' => array('alias' => 'british_airways', 'name' => 'British Airways')
);
$body['{{body}}'] = 'Line one';
foreach ($carrier as $key=>$value) {
$body['{{body}}'] .= '<option value=' . $value['alias'] . '>' . $value['name'] . '</option>';
}
$body['{{body}}'] .= 'Line two';
$body['{{body}}'] .= 'Line three';
$body['{{body}}'] .= 'Line four';
print_r($body);
这篇关于PHP:在foreach循环中连接一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文