我有两个阵列。我想要做的是比较两个数组的键['user_id'],如果它们相同,则以隐藏的形式传递['user_id']和['ref']。我试图将它们分成两个foreach,但系统似乎陷入了死锁。
<?php foreach($_SESSION['printing_set'] as $data) { ?>
<?php foreach(getProvenaMailableUserlist() as $userlist){ ?>
<input type="hidden" name="reference[<?php echo $data['user_id'] ?>]" value="<? if($userlist['user_id'] == $data['user_id']){echo $userlist['ref'];} ?>" />
<?php } ?>
<?php } ?>
这样做的正确方法是什么?
答案 0 :(得分:1)
你正在做的是一次又一次地打印'&lt; input type =“hidden”name =“...'的部分。这是你应该做的事情
<?php
echo '<input type="hidden" name="reference[' . $data['user_id'] . ']" value="'; //olny one time.
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
if($userlist['user_id'] == $data['user_id']){
echo $userlist['ref']; //only if condition is true
}
}
}
echo '" />'; //only one time
?>
答案 1 :(得分:0)
你有一些时髦的格式,所以很难分辨出错误的位置。试试这样:
<?php
foreach($_SESSION['printing_set'] as $data) {
foreach(getProvenaMailableUserlist() as $userlist){
$value = "";
if($userlist['user_id'] == $data['user_id'])
$value = $userlist['ref'];
echo "<input type='hidden' name='reference$user_id' value='$value' /> \n";
}
}
?>