HI以下是代码,我正在尝试打印包含下拉菜单值的数组 然后第二个foreach循环试图选择该字段。 输出是我想要的方式,意味着它打印所有菜单项,以及所选的一个,
foreach($results as $data):
$st = '';
foreach($SelectedActor as $SelectedActor):
if($SelectedActor['id']==$data['id']){$st='selected="selected"';}
endforeach;
$dd .="<option ".$st." value=".$data['id']."> ".$data['artist_name']." </option>";
endforeach;
但该页面也显示此错误
A PHP Error was encountered
Severity: Warning
Message: Illegal string offset 'id'
Filename: controllers/replik.php
Line Number: 328
这两个阵列的Vardump是
array (size=2)
0 =>
array (size=2)
'id' => string '1' (length=1)
'artist_name' => string ' oyuncu' (length=19)
1 =>
array (size=2)
'id' => string '4' (length=1)
'artist_name' => string 'hep brabir' (length=10)
array (size=2)
0 =>
array (size=2)
'id' => string '1' (length=1)
'artist_name' => string 'oyuncu' (length=19)
1 =>
array (size=2)
'id' => string '4' (length=1)
'artist_name' => string 'hep brabir' (length=10)
任何人都可以帮我解决这个错误。
答案 0 :(得分:2)
foreach($SelectedActor as $SelectedActor):
if($SelectedActor['id']==$data['id']){$st='selected="selected"';}
这里使用相同名称$SelectedActor
,也许
foreach($SelectedActor as $ItemSelectedActor):
if($ItemSelectedActor['id']==$data['id']){$st='selected="selected"';}
没问题
答案 1 :(得分:0)
下面的行是错误的:
foreach($SelectedActor as $SelectedActor)
你需要这样做:
foreach($SelectedActor as $actor):
if($actor['id']==$data['id']){
$st='selected="selected"';
}
endforeach;