多维关联数组 - 删除重复项

时间:2021-01-27 14:48:25

标签: php mysql arrays multidimensional-array

我有一个 MySQL 查询,我运行了一个 while 循环,它为我提供了一个多维关联数组。

$query2 = "SELECT DISTINCT 
            soc.rowid as id,
            fc.paye as paid
            FROM invoices fc
            LEFT JOIN customers soc on fc.fk_soc = soc.rowid
            AND fc.paidBy < '" . $date . "'";

$unpaidInv = $db->query($query2);

while($row1 = mysqli_fetch_array($unpaidInv, MYSQLI_ASSOC)){
    $params1[] = $row1;
}
var_dump($params1);

var_dump 结果如下:

array(10) {                                                                                                                                                                                                        
  [0]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "19"                                                                                                                                                                                                 
    ["paid"]=>                                                                                                                                                                                                     
    string(1) "1"                                                                                                                                                                                                  
  }                                                                                                                                                                                                                
  [1]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "22"                                                                                                                                                                                                 
    ["paid"]=>                                                                                                                                                                                                     
    string(1) "1"                                                                                                                                                                                                  
  }                                                                                                                                                                                                                
  [2]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "21"                                                                                                                                                                                                 
    ["paid"]=>                                                                                                                                                                                                     
    string(1) "0"                                                                                                                                                                                                  
  }                                                                                                                                                                                                                
  [3]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "23"                                                                                                                                                                                                 
    ["paid"]=>                                                                                                                                                                                                     
    string(1) "1"                                                                                                                                                                                                  
  }                                                                                                                                                                                                                
  [4]=>                                                                                                                                                                                                            
  array(2) {                                                                                                                                                                                                       
    ["id"]=>                                                                                                                                                                                                       
    string(2) "21"                                                                                                                                                                                                 
    ["paid"]=>
    string(1) "1"
  }
  [5]=>
  array(2) {
    ["id"]=>
    string(2) "32"
    ["paid"]=>
    string(1) "1"
  }
  [6]=>
  array(2) {
    ["id"]=>
    string(2) "22"
    ["paid"]=>
    string(1) "0"
  }
  [7]=>
  array(2) {
    ["id"]=>
    string(2) "23"
    ["paid"]=>
    string(1) "0"
  }
  [8]=>
  array(2) {
    ["id"]=>
    string(2) "32"
    ["paid"]=>
    string(1) "0"
  }
  [9]=>
  array(2) {
    ["id"]=>
    NULL
    ["paid"]=>
    string(1) "0"
  }
}
string(120) "

目前存在具有不同“付费”值的相同 id 的数组项。 例如,数组项 2id->21paid->0 – 数组项 4、< strong>id->21,付费->1

我需要我的数组只返回全为 1 的结果。所以如果数组有 id->21 两次,但付费项目有 paid->1 和 < strong>paid->0 它不应该出现在数组中。

只有全为 1 的数组项。

2 个答案:

答案 0 :(得分:1)

我会在请求中尝试这样的事情:

SELECT DISTINCT 
        soc.rowid as id,
        fc.paye as paid
        FROM invoices fc
        LEFT JOIN customers soc on fc.fk_soc = soc.rowid
        WHERE fc.paidBy < '" . $date . "'"
        AND soc.rowid NOT IN (SELECT DISTINCT 
        soc2.rowid as id,
        FROM invoices fc2
        LEFT JOIN customers soc2 on fc2.fk_soc = soc2.rowid
        WHERE fc2.paidBy < '" . $date . "'"
        AND fc2.paye = '0');

为了更精确地适应,但考虑到这一点,您将只保留不在子查询中的行,该子查询检索具有零值的行

答案 1 :(得分:0)

对于任何对可用解决方案感兴趣的人,请参阅下文。

SELECT DISTINCT
soc.rowid as id
FROM invoices fc
LEFT JOIN customers soc on fc.fk_soc = soc.rowid
WHERE soc.rowid IS NOT NULL
AND fc.paidBy < '" . $date . "'"
AND soc.rowid NOT IN(
SELECT DISTINCT
soc.rowid as id
FROM invoices fc
LEFT JOIN customers soc on fc.fk_soc = soc.rowid
AND fc.paye = '0'
WHERE soc.rowid IS NOT NULL
AND fc.paidBy < '" . $date . "'");