Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维关联数组-删除重复项_Php_Mysql_Arrays_Multidimensional Array - Fatal编程技术网

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

Php 多维关联数组-删除重复项,php,mysql,arrays,multidimensional-array,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 <

我有一个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);
当前,相同id的数组项具有不同的支付值。 例如,数组项2,id->21,paid->0–数组项4,id->21,paid->1

我需要数组只返回全部为1的结果。因此,如果数组有两次id->21,但支付的项目已支付->1和支付->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');

为了更精确地进行调整,但要记住这一点,您将只保留不在子查询中的行,该子查询检索值为零的行。对于任何对可用解决方案感兴趣的人,请参见下面的内容

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 . "'");

SQL中的fc.paye=1是否是最佳方式?否,因为它必须在数据集中的所有事件中运行,以从结果数组中排除已支付1和已支付0的id。因此,对于已经支付了1和0的ID,它应该是错误的,而ID的多个支付为1或只需支付1个。需要重新排列数组以实现结果。并且请考虑将准备好的语句切换到。是的,可以重构数组。谢谢@El_Vanja。
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 . "'");