如何在每个循环中为一个字符串连接?我在下面做的事情在没有循环的情况下可以正常工作,但有没有办法可以将循环放在那里而不会结束$body[{{body}}]
并再次启动它?
$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);
答案 0 :(得分:2)
试试这个 - 你必须连接foreach循环中生成的每一行。
$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);
答案 1 :(得分:1)
$word = 'foo';
$result = "";
$char_buff = str_split($word);
foreach ($char_buff as $char){
$result .= $char;
}
echo var_dump($result);
输出以下内容:
string(3) "foo"
How to combine strings inside foreach into single string PHP