PHP mysql array_diff与所有字段都不匹配

时间:2016-06-09 12:54:27

标签: php mysql arrays

旧场(截止)

| exceptions    |
+---------------+
| ryu,moderator |

新字段(帐户)

| username      |
+---------------+
| ben           |
+---------------+
| moderator     |
+---------------+
| john          |
+---------------+
| james         |
+---------------+
| ryu           |

我想删除重复的字段,但下面的代码不起作用(样本)

$expr_old = explode(',', $exceptions);
$expr_new = explode(',', $newfields);

echo implode(array_diff($expr_new, $expr_old), ',');

完整代码

$query = "SELECT ACC.username AS username, SAL.income AS income, CDIR.exceptions AS except ";
$query .= "FROM accounts ACC ";
$query .= "LEFT JOIN info INF ON ACC.user_id = INF.iuid ";
$query .= "LEFT JOIN sales SAL ON ACC.user_id = SAL.suid ";
$query .= "LEFT JOIN cutoff_direct CDIR ON ACC.username = CDIR.user";

$s = $sqlConnection->query($query);

while ($row = $s->fetch_assoc())
{
    $ss = $sqlConnection->query("SELECT username FROM accounts WHERE sponsor = '$row[username]'");

    $exceptions = '';

    while ($rrow = $ss->fetch_assoc())
    {
        $exceptions .= $rrow['username'] . ',';
    }

    $expr_old = explode(',', $row['except']);
    $expr_new = explode(',', $exceptions);

    echo implode(array_diff($expr_new, $expr_old), ',');
}

输出:用户名ryu仍为

我的问题是一些重复的用户意外出现。

1 个答案:

答案 0 :(得分:2)

我执行了以下代码,输出符合预期。

$exceptions = 'ryu,moderator';
$newfields = 'ben,moderator,john,james,ryu';
$expr_old = explode(',', $exceptions);
$expr_new = explode(',', $newfields);
echo implode(array_diff($expr_new, $expr_old),',');

输出: -

ben,john,james

请检查您正在爆炸的字符串是否包含任何空格。单词只能用逗号分隔。