简体   繁体   English

比较数组中的条目并删除重复项(PHP,MySQL)

[英]Comparing entries in an array and removing duplicates (PHP, MySQL)

I have this little task I'm trying to accomplish in PHP/MySQL. 我有这个小任务要在PHP / MySQL中完成。

We're collecting fax-numbers (and other data) from customers that we'll use in case we have a problem with our operations at the company I work for (not in programming, don't worry). 我们正在从客户那里收集传真号码(和其他数据),以防万一我所工作的公司的运营出现问题(不用编程,不用担心)。

Basically I have an array that looks something like this. 基本上我有一个看起来像这样的数组。

   Array
   [1]
      [company] => "Company A"
      [fax] => "031-558330"

   [2]
      [company] => "Company B"
      [fax] => "031558330"

   [3]
      [company] => "Company C"
      [fax] => "12345"

Note that [fax] is actually a duplicate in 1 and 2 if you remove the dash (-). 请注意,如果您删除破折号(-),则[传真]实际上是1和2中的重复项。

My problem is that I need to remove [2], because it's a duplicate of [1]. 我的问题是我需要删除[2],因为它是[1]的副本。 Basically, I'm trying to list unique fax numbers and print the company name of that unique fax number. 基本上,我试图列出唯一的传真号码并打印该唯一的传真号码的公司名称。

So far I've done this: 到目前为止,我已经做到了:

function format_text($text,$number)
{
    $text = str_ireplace(" ","",$text);
    $text = str_ireplace("-","",$text);
    $text = str_ireplace(",","",$text);
    $text = str_ireplace("/","",$text);
    $text = str_ireplace("+46","0",$text);
    $text = trim($text);

    return $text;
}


$fax_result = mysql_query("SELECT fax,company FROM beredskap GROUP BY company") or die(mysql_error());

for($i = 0; $fax_array[$i] = mysql_fetch_assoc($fax_result); $i++) ;
array_pop($fax_array);

for($i = 0; $i<count($fax_array);$i++)
{ 
    $fn = format_text($fax_array[$i]['fax']);
    if(!empty($fn)) {
    echo trim($fax_array[$i]['company']) .",". $fn ."\n";

    }
}

I have no clue as to how I'm supposed to compare two subelements (as the raw data from the SQL might have differences in them, but when stripped of dashes and commas is duplicate to other entries), and then remove the parent key. 我不知道应该如何比较两个子元素(因为来自SQL的原始数据可能在其中有所不同,但是当去除破折号和逗号时,它们会复制到其他条目),然后删除父键。

I sincerely hoped I've posed this question in the correct fashion. 我衷心希望我以正确的方式提出了这个问题。

Anyone willing to help out? 有人愿意帮忙吗? Thank you! 谢谢!

You can do it in SQL: 您可以在SQL中执行此操作:

SELECT   company,
         TRIM(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(fax,
           ' ',   '' ),
           '-',   '' ),
           ',',   '' ),
           '/',   '' ),
           '+46', '0')
         ) AS faxno
FROM     beredskap
GROUP BY faxno

Note that where the same fax number appears in the table against different companies, the above query will select one of those companies indeterminately. 请注意,如果表格中针对不同公司的传真号码相同,则上面的查询将不确定地选择这些公司之一。 If in such cases you would prefer to choose a particular company, please clarify how one determines the company that is to be selected. 在这种情况下,如果您希望选择一家特定的公司,请说明一个人如何确定要选择的公司。

Note also that this grouping will result in multiple entries in the resultset for a single company if that company has records with different fax numbers in the beredskap table, whereas your original query would have indeterminately chosen only one of such company's fax numbers. 还要注意,如果该公司在beredskap表中具有不同传真号的记录,则该分组将在单个公司的结果集中产生多个条目,而您的原始查询将不确定地选择了该公司的传真号之一。 If this is not your desired behaviour, please clarify how one determines the fax number that is to be selected. 如果这不是您想要的行为,请说明一个人如何确定要选择的传真号码。

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

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