简体   繁体   English

PHP的array_unique结合array_filter,错误的输出

[英]php array_unique combined with array_filter, incorrect output

In a database table are multiple entries related to a customer. 数据库表中有多个与客户相关的条目。 Each entry will have time_1, time_2, time_3, time_4, time_5 and time_6 fields. 每个条目将具有time_1,time_2,time_3,time_4,time_5和time_6字段。 I have looped through the entries and used this to combine these times to create a round cycle for a day: 我遍历了条目,并使用它们来结合这些时间以创建一天的循环周期:

while($row=mysql_fetch_assoc($script_check))
        {
            $prerounds = array_filter($row);
            $rounds = array_unique(array_merge($prerounds));    
        }

This works however if one entry has times 1-4 set and the second entry has times 1-3 set the fourth time will be totally removed thus only times 1-3 are put into the array. 但是,如果一个条目的时间设置为1-4,第二个条目的时间设置为1-3,则此方法有效,第四次将被完全删除,因此仅将1-3放入数组中。 How can I combine these fields without losing data when some are blank? 当某些字段为空白时,如何合并这些字段而不会丢失数据? This strategy so far has been to strip the $row of all blanks then merge it together to create one set of round times related to that customer for that day. 到目前为止,这种策略是剥离所有空白行的美元,然后将其合并在一起,以创建当天与该客户相关的一组舍入时间。 Thanks. 谢谢。

As the OP requested: 如操作人员要求:

Basically, your main problem is you're re-initializing $rounds on each iteration, you're not merging any existing array with a new one. 基本上,您的主要问题是您要在每次迭代中重新初始化$rounds ,而不是将任何现有数组与新数组合并。

$rounds = array();
while($row = $pdo->fetch(PDO::FETCH_ASSOC))
{
    $rounds = array_merge($rounds, array_unique(array_filter($row)));
}

Hmz... apart from mysql_* being deprecated (that's why I used PDO in my snippet), you should really check your logic... perhaps a decent query might be more suitable, or a simple set of if 's. 嗯...除了不推荐使用mysql_* (这就是为什么我在代码段中使用PDO的原因)之外,您还应该检查一下自己的逻辑...也许像样的查询可能更合适,或者是一组简单的if It's even likely to be faster that way 这样甚至可能更快

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

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