我正在尝试在foreach循环之外创建一个变量,包含它在循环中获取的所有内容。
我在这里做错了什么?
$fields = array();
foreach( $submit_fields as $key => $items ) {
if( $items->value == true ) {
$fields = " <div class='control-group'>
<label>" . $items->label . "</label>
<div class='controls'>
<input type='text' class='form-control input-small' name='" . $items->name . "'>
</div>";
}
}
print_r( $fields );
答案 0 :(得分:3)
将$fields
更改为以下内容:
$fields[] = "
<div class='control-group'>
<label>" . $items->label . "</label>
<div class='controls'>
<input type='text' class='form-control input-small' name='" . $items->name . "'>
</div>";
}
答案 1 :(得分:0)
您的代码需要
$fields = array();
foreach($submit_fields as $key => $items) {
if($items->value == true) {
$fields[] = "
<div class='control-group'>
<label>" . $items->label . "</label>
<div class='controls'>
<input type='text' class='form-control input-small' name='" . $items->name . "'>
</div>";
}
}
print_r($fields);
您还可以通过更改将数组与键值相关联 $域[] 至 $ fields [$ key]