【发布时间】:2021-01-02 08:44:42
【问题描述】:
我想在 html 中进行如下选择显示:
<div name="option_a">
<p>result for option_a</p>
<img src="option_a_image"></img>
</div>
<div name="option_b">
<p>result for option_b</p>
<img src="option_b_image"></img>
</div>
<div name="option_c">
<p>result for option_c</p>
<img src="option_c_image"></img>
</div>
<div name="option_d">
<p>result for option_d</p>
<img src="option_d_image"></img>
</div>
使用这个数组:
Array
(
[0] => stdClass Object
(
[option_a] => its option a
[option_a_image] => its an image for option a
[option_b] => its option b
[option_b_image] => its an image for option b
[option_c] => its option c
[option_c_image] => its an image for option c
[option_d] => its option d
[option_d_image] => its an image for option d
)
[1] => stdClass Object
(
[option_a] => its option a
[option_a_image] => its an image for option a
[option_b] => its option b
[option_b_image] => its an image for option b
[option_c] => its option c
[option_c_image] => its an image for option c
[option_d] => its option d
[option_d_image] => its an image for option d
)
[2] => stdClass Object
(
[option_a] => its option a
[option_a_image] => its an image for option a
[option_b] => its option b
[option_b_image] => its an image for option b
[option_c] => its option c
[option_c_image] => its an image for option c
[option_d] => its option d
[option_d_image] => its an image for option d
)
)
但我想使用循环使其更简洁。我尝试了以下方法:
<?php foreach($array[0] as $type=>$value ){
if(strpos($type,'option_') !== false){ ?>
<div class="card mb-3" style="width: 98%; margin: auto">
<div class="card-body">
<label ><input type='radio' name='r1' tabindex='5'> <span><?php echo $value ?></span></label> <br>
</div>
</div>
<?php } } ?>
但是像这样的循环不能产生我想要创建的视图。 那么如何让循环形成我想要产生的结果呢?
谢谢
【问题讨论】: