Php 使用数组中的值动态生成输入
我希望使用与第二个数组值相对应的值动态地进行输入 守则:Php 使用数组中的值动态生成输入,php,Php,我希望使用与第二个数组值相对应的值动态地进行输入 守则: $first = array('location', 'genre', 'studio', 'Lord_Of_the_Rings'); $second = array( 'location' => 1, 'genre' => 2, 'studio' => 3, 'Lord_Of_the_Rings' => 4 ); $intersect = array_intersect(
$first = array('location', 'genre', 'studio', 'Lord_Of_the_Rings');
$second = array(
'location' => 1,
'genre' => 2,
'studio' => 3,
'Lord_Of_the_Rings' => 4
);
$intersect = array_intersect($first, array_keys($second));
foreach($intersect as $key) {
$t = $second[$key];
}
foreach ($first as $the_tax) {
$do_val = $the_tax.'-no-';
echo "<p>$the_tax <input type=\"text\" name=\"{$do_val}\" value=\"$t\" /></p>";
}
$first=array('location'、'genre'、'studio'、'Lord_Of the_Rings');
$second=数组(
“位置”=>1,
“流派”=>2,
“工作室”=>3,
“指环王”=>4
);
$intersect=array_intersect($first,array_keys($SEXT));
foreach($intersect as$key){
$t=$second[$key];
}
foreach($第一个作为$U税){
$do_val=$u税。“-否-”;
回声“$the_tax”;
}
这将产生:
<p>location <input type="text" value="4" name="location-no-"></p>
<p>genre <input type="text" value="4" name="genre-no-"></p>
<p>studio <input type="text" value="4" name="studio-no-"></p>
<p>Lord_Of_the_Rings <input type="text" value="4" name="Lord_Of_the_Rings-no-"></p>
位置
体裁
工作室
魔戒之王
如您所见,每个输入的值为4。
在这种特殊情况下,输入中的值应该是第一个为
1
,第二个为2
,第三个为3
,第四个为4
,但我无法实现这一点。您在foreach中设置$t,但当它进入下一个循环时,它只会有最后一个值
你能不能不这样做:
$first = array('location', 'genre', 'studio', 'Lord_Of_the_Rings');
$second = array(
'location' => 1,
'genre' => 2,
'studio' => 3,
'Lord_Of_the_Rings' => 4
);
foreach ($first as $the_tax) {
$do_val = $the_tax.'-no-';
echo "<p>$the_tax <input type=\"text\" name=\"{$do_val}\" value=\"{$second[$the_tax]}\" /></p>";
}
$first=array('location'、'genre'、'studio'、'Lord_Of the_Rings');
$second=数组(
“位置”=>1,
“流派”=>2,
“工作室”=>3,
“指环王”=>4
);
foreach($第一个作为$U税){
$do_val=$u税。“-否-”;
回声“$the_tax”;
}
是!虽然$first应该是$second。不错!您应该考虑使用一个数组而不是两个数组,如<代码> $Frase=数组(“位置”=>数组(‘值’=>‘1’));<代码>