【问题标题】:array_unique not removing duplicate values phparray_unique 不删除重复值 php
【发布时间】:2018-05-02 19:52:50
【问题描述】:

抱歉,如果这是重复的,我已尝试搜索但似乎找不到答案。我可能只是把这段代码放错了地方。

我已经计算了从各种选择框中输入的重复值,这些值是通过 $_GET 发送过来的。使用这些重复项,如果超过(无论设置的数量是多少),那么它将通过 mysql 查询运行。这一切都很好。

问题是我需要删除从 mysql 查询返回的重复项。这是我的代码:

if ($countGearSelected >= 2) {      
        $gearSets = array_keys(array_filter(array_count_values($_GET['gearPiece']), function($v) {
            return $v > 1;
        }));

        foreach ($gearSets as $gearSetKey => $gearSetValue) {

            $result = mysqli_query($con,"SELECT twoPieceBonus FROM sets WHERE setName='".$gearSetValue."';");
            while($row = mysqli_fetch_array($result)){

                $twoPieceBonus .= urldecode($row['twoPieceBonus']).'</br></br>';

            }
            $twoPieceBonus = implode(',',array_unique(explode(',', $twoPieceBonus)));
            $twoSelected = substr($twoPieceBonus, 0, -10);

        }


    }else{
        $twoSelected = '';
    }

如您所见,我在 SE 上的其他各种帖子中尝试了 array_unique 选项,但它似乎不起作用。我想我可能用错了?

在 mysql 查询中使用 DISTINCT 不起作用,因为正在查询的一些“集合”具有相同的结果(如果这有意义?)。

非常感谢任何帮助。

【问题讨论】:

  • 你能给我们$result中值的格式吗?
  • 你能打印出你的 $result 值吗?
  • mysqli_result 对象([current_field] => 0 [field_count] => 1 [lengths] => [num_rows] => 1 [type] => 0)
  • “它似乎不起作用”:这很模糊。 array_unique 当然可以,所以请提供一个具体的数据示例,你从中得到了什么,以及它与预期结果有何不同。
  • 用逗号 (,) 替换
    标记或用
    分解。

标签: php mysql arrays string duplicates


【解决方案1】:

首先:您的代码容易受到SQL injection 的攻击:使用prepared statements 来避免这种情况。

其次,在循环的每次迭代中执行查询通常是一个坏主意。在这种情况下,它可以避免。您可以使用 in 运算符并一次性比较所有齿轮组,而不是在您的 where 子句中进行相等比较。

这也将解决获取不同值的问题。只执行一个查询,您现在可以使用distinct

代码如下所示。我无法对此进行测试,但我希望可以轻松修复错误(如果有):

$twoSelected = '';
if ($countGearSelected >= 2) {      
    $gearSets = array_keys(array_filter(
        array_count_values($_GET['gearPiece']), function($v) {
            return $v > 1;
        }
    ));

    // Create comma separated list of question marks
    $placeHolders = implode(",", array_fill(0, count($gearSets), "?"));
    // Prepare SQL statement with it
    $stmt = mysqli_prepare($con, 
            "SELECT DISTINCT twoPieceBonus 
             FROM sets 
             WHERE setName IN ($placeHolders);");

    // All gearSet values are strings:
    $types = str_repeat("s", count($gearSets));
    // Turn the gearSets into references
    $gearSetRefs = [];
    foreach ($gearSets as $i => $_) {
         $gearSetRefs[] = &$gearSets[$i];
    }
    // Bind arguments
    mysqli_stmt_bind_param($stmt, $types, ...$gearSetRefs); // the splat operator
    // Now we are all set to (safely) execute the query 
    mysqli_stmt_execute($stmt);
    $result = mysqli_stmt_get_result($stmt);
    // Let the result of the URL decoding still be an array
    $twoPieceBonus = [];
    while ($row = mysqli_fetch_array($result)) {
        $twoPieceBonus[] = urldecode($row['twoPieceBonus']);
    }
    mysqli_stmt_close ($stmt);
    // ... and then use implode to insert those HTML breaks
    $twoSelected = implode("</br></br>", $twoPieceBonus);
}

【讨论】:

  • 感谢您的建议,它似乎马上就奏效了!唯一的问题(可能是另一个问题,因为它可能会变得很长)是,在我的 6 个选择中(对于齿轮组),如果您从一个齿轮组中选择 2 个,从另一个齿轮组中选择 4 个,它会输出fourPieceBonus两者(此查询再次迭代 3 件和 4 件奖金)
  • 如果你在另一个循环中有这段代码,你需要将结果存储在一个简单的变量中(比如$twoSelected),而是将它们添加到一个数组中,以便在外部循环之后完成后,您仍然可以访问单个结果。但是,是的,可能我过于简单化了你试图告诉我的内容,所以也许可以提出一个提供所有细节的新问题。
  • 会的。非常感谢你的帮助。我现在将发布另一个问题。非常感谢:)
  • 关于您的答案的快速问题,很抱歉一直打扰您...我需要在每次迭代后释放结果还是关闭它?在这个运行之后,它似乎不想与代码的其他迭代一起工作,我相信这可能是原因
  • 是的,使用mysqli_stmt_close ($stmt)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多