简体   繁体   English

在foreach中从两个单独的数组中提取两个对象

[英]Pulling two objects from two separate arrays in a foreach

I'm sure I'm over-thinking this, any help to steer me straight is appreciated. 我敢肯定,我想得太过分了,感谢任何帮助我直率的帮助。 I need to pull two array variables and place them in a link so that I can I can apply them to another form in order to change their respective arrays (these links are used to pull recipient names and their uid values that are stored in a session arrays.) So, in my foreach, the "$contactlistunique as $rec" line seems to work fine in order to pull the username, but now I need to figure out a way to pull the uid object. 我需要提取两个数组变量并将其放置在链接中,以便可以将它们应用于另一种形式,以便更改它们各自的数组(这些链接用于提取存储在会话中的收件人名称及其uid值因此,在我的foreach中,“ $ contactlistunique as $ rec”行似乎可以正常工作以提取用户名,但现在我需要找出一种提取uid对象的方法。 Foreach operations don't allow for multiple conditions, so what's the smartest way to do this? Foreach操作不允许多个条件,那么最明智的方法是什么?

<?php
$contactlistuidunique = array_unique($_SESSION['recipientlist']);
$contactlistunique = array_unique($_SESSION['contactlist']);
foreach ($contactlistunique as $rec)
    {
    echo "<font color=#808080><a href='removecontact.php?contact=$recuid&recipient=$rec' STYLE='TEXT-DECORATION: NONE'>
    <font color=#808080>" . $rec . "</a></font>";
    }
?>

Try it though the question is not much clear to me 尝试一下,尽管这个问题对我来说不是很清楚

<?php
$contactlistuidunique = array_unique($_SESSION['recipientlist']);
$contactlistunique = array_unique($_SESSION['contactlist']);
foreach ($contactlistunique as $key=>$rec)
{
    echo "<font color=#808080><a href='removecontact.php?contact=".$contactlistuidunique[$key]."&recipient=$rec' STYLE='TEXT-DECORATION: NONE'>
    <font color=#808080>" . $rec . "</a></font>";
}
?>

Based on you comments, I advice you not to use array_unique, but do the following: 根据您的评论,我建议您不要使用array_unique,但请执行以下操作:

<?php
$isBefore = array();
foreach ($_SESSION['contactlist'] AS $key => $rec)
{
  if (!in_array($rec, $isBefore)) {
    $isBefore[] = $rec;
    echo "<font color=#808080><a href='removecontact.php?contact=" . $_SESSION['recipientlist'][$key] . "&recipient=$rec' STYLE='TEXT-DECORATION: NONE'><font color=#808080>" . $rec . "</a></font>";
  }
}

So, only unique values are displayed and keys are preserved. 因此,仅显示唯一值并保留键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM