【问题标题】:Remove duplicates from PHP array (array_unique)从 PHP 数组中删除重复项 (array_unique)
【发布时间】:2016-01-06 15:57:57
【问题描述】:

我从 4 个不同的表中获取特定字段。

<?php 

//I connect to the database and the 4 tables

// Location of CSV  
$location = 'path.csv';

// List creation that will be updated with the fields and be put into my CSV file
$list = array();

// Read csv file to avoid adding duplicates
$file = fopen($location, 'r');
$data = array();

while($row = fgetcsv($file)) 
{
   $data[] = $row;
}

// Query 1
$sql = ('select distinct(field) as field from '.$table1.'');

// Run the query
$query = $Db->query($sql);

// Check for SQL errors
if ($Db->error) 
{
    return ($Db->error);
}

// Put data in the list
while ($row = $query->fetch_assoc()) 
{
    array_push($list,array($row['field'], ''));
}

// Query 2
$sql = ('select distinct(field) as field from '.$table2.'');

// Run the query
$query = $Db->query($sql);

// Check for SQL errors
if ($Db->error) 
{
    return ($Db->error);
}

// Put data in the list
while ($row = $query->fetch_assoc()) 
{
    array_push($list,array($row['field'], ''));
}

// Query 3
$sql = ('select distinct(field) as field from '.$table3.'');

// Run the query
$query = $Db->query($sql);

// Check for SQL errors
if ($Db->error) 
{
    return ($Db->error);
}

// Put data in the list
while ($row = $query->fetch_assoc()) 
{
    array_push($list,array($row['field'], ''));
}

// Query 4
$sql = ('select distinct(field) as field from '.$table4.'');

// Run the query
$query = $Db->query($sql);

// Check for SQL errors
if ($Db->error) 
{
    return ($Db->error);
}

// Put data in the list
while ($row = $query->fetch_assoc()) 
{
    array_push($list,array($row['field'], ''));
}


// Save list in the csv file without overwriting
$fp = fopen($location, 'a');

foreach (array_unique($list) as $fields) 
{
    if (in_array($fields, $data)) 
    {
        echo "Duplicate found";
    }
    else
    {
        echo "Save to file";
        fputcsv($fp, $fields);
    }           
}

fclose($fp);    

?>

最后我检查这些字段是否已经在文件中。唯一的问题是我仍然有重复项,因为某些表可能具有完全相同的字段。所以,我想从 PHP 数组“列表”中删除重复项。

我正在使用:

$cleanlist = array_unique($list);

但我收到一个错误:

PHP注意事项:数组到字符串的转换

更具体地说,我的代码中的更改是:

    $cleanlist = array_unique($list);

// Save list in the csv file without overwriting
$fp = fopen($location, 'a');

foreach ($cleanlist as $fields) 
{
    if (in_array($fields, $data)) 
    {
        echo "Duplicate found";
    }
    else
    {
        echo "Save to file";
        fputcsv($fp, $fields);
    }           
}

【问题讨论】:

    标签: php array-unique


    【解决方案1】:

    正如the docs 中解释的那样,array_unique 默认将元素作为字符串进行比较。您收到此错误是因为 PHP 正在尝试将数组转换为字符串。你有一个二维数组,一个数组数组。

    您可以使用标志SORT_REGULAR 来比较元素的原样。 但请注意,只有相同的键/值对才会被视为相同。

    【讨论】:

      【解决方案2】:

      在 SELECT 语句中使用 UNION 可以大大减少代码量。

      SELECT field FROM table1
      UNION
      SELECT field FROM table2
      UNION
      SELECT field FROM table3
      UNION
      SELECT field FROM table4
      

      UNION 默认返回不同的结果。

      【讨论】:

      • 很好的解决方案。它节省了大量的行!我也发布了一个答案,但你的更好。我的只是将 array_unique 用于 n 维表的一种方式。
      【解决方案3】:

      成功了:

      $list = array_map("unserialize", array_unique(array_map("serialize", $list)));
      

      $list 是一个二维数组。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-05-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-21
        • 1970-01-01
        相关资源
        最近更新 更多