在Foreach循环中删除重复项 [英] Removing Duplicates in Foreach Loop

查看:119
本文介绍了在Foreach循环中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了其他类似的问题,但实际上却完全不同,因此我发布了自己的版本.下面的代码(尤其是表单)经过简化,以更好地适应.

I saw other questions that seemed like this one but they were actually quite different so I'm posting my own version. The code below, especially the form, was simplified so as to better fit.

此查询是动态生成的,但由于使用六个单独的字段提交日期和时间(对于人类可读日期的每个部分而言一个字段)来提交,因此具有重复项.然后,一个自定义函数将它们放入Unix时间戳中进行插入,但是由于它位于foreach循环中,因此它会复制该字段.如何删除重复项?

This query is being generated dynamically but has duplicates due to the fact that the date and time are being submitted using six individual fields, one for each portion of the human-readable date. Then a custom function puts these into a Unix timestamp for insertion but because it is within a foreach loop, it duplicates the field. How can I remove the duplicates?

UPDATE tablename SET 
`Name`='First Last',
`EMail`='email@saddress.com',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
`DateUpdated`='1544081955',
 WHERE ID='9'

这是函数的一部分,以 if(Contains("Date",$ key))作为条件的一部分,正在处理六个选择器以及问题所在.一切正常,但我想摆脱多余的DateUpdate值!

This is a portion of the function and the conditional starting with if (Contains("Date", $key) is where the six selectors are being processed and where the problem is. To clarify it is all working but I want to get rid of the extra DateUpdate values!

if (isset($_POST['update'])) :
            unset($_POST['update']);
            // Remove unneeded fields specified in $RemoveFields variable
            if (isset($RemoveFields) && !is_array($RemoveFields)) $RemoveFields = array($RemoveFields);
            $filteredarray = array_diff_key($_POST, array_flip($RemoveFields));
            foreach ($filteredarray as $key=>$value ) :
                if ($key === 'ID') continue;
                if ($key === 'VerifyCode') continue;
                // Process any password field
                if (Contains("Pass", $key)) :
                    // Encode password field
                    if ($value !== "") $value=md5($value);
                    // If no changes, save original password
                    if ($value === "") continue;
                endif;
                // Process date and time selectors
                if (Contains("Date", $key) && is_numeric($value)):
                    $removals = array('year','month','day','hour','minute','second');
                    $FieldName = trim(str_replace($removals,"",$key));
                    $key = $FieldName;
                    $value=dateProcess($FieldName,"Unix");
                endif;
                // Prepare array for query
                $Values[] = "`$key`=".isNull($value, $DBName);
            endforeach;
            $sqlUpdate = "UPDATE $TableName SET ".implode(",",$Values)
                                    ." WHERE ID='".intval($PostID)."'";
endif;

最后,这是表格的简化版本

And finally, here is a simplified version of the form

<form method="POST" name="SendMessage" action="formname.php">
<fieldset>
<legend>Form Name</legend>
<p><label for="Name">Full Name</label>
<input type="text" name="Name" value="Full Name" size="32" class="Input" id="Name">

<p><label for="EMail">EMail</label>
<input type="text" name="EMail" value="email@address.com" size="32" class="Input" id="EMail"> 

<p><label for="DateUpdated">Date Updated</label>
<select name="monthDateUpdated" id="monthDateUpdated">
<option value=""></option>
<option value="12" SELECTED>December</option>
</select>

<select name="dayDateUpdated" id="dayDateUpdated">
<option value=""></option>
<option value="06" SELECTED>06</option>
</select>

, <select name="yearDateUpdated" id="yearDateUpdated">
<option value=""></option>
<option value="2018" SELECTED>2018</option>
</select>

 at <select name="hourDateUpdated" id="hourDateUpdated">
<option value=""></option>
<option value="01" SELECTED>01</option>
</select>

:<select name="minuteDateUpdated" id="minuteDateUpdated">
<option value=""></option>
<option value="36" SELECTED>36</option>
</select>

:<select name="secondDateUpdated" id="secondDateUpdated">
<option value=""></option>
<option value="59" SELECTED>59</option>
</select>

<input type="hidden" name="ID" value="9"> 
<p><div class="ButtonCenter">
<input name="update" type="submit" value="Save Changes">
</div>

</fieldset>
</form>

推荐答案

删除此内容:

// Process date and time selectors
if (Contains("Date", $key) && is_numeric($value)):
    $removals = array('year','month','day','hour','minute','second');
    $FieldName = trim(str_replace($removals,"",$key));
    $key = $FieldName;
    $value=dateProcess($FieldName,"Unix");
endif;

替换为:

if (Contains("Date", $key) && is_numeric($value)):
    $removals = array('year','month','day','hour','minute','second');
    $FieldName = trim(str_replace($removals,"",$key));
    $key = $FieldName;
    $time_val = $filteredarray['yearDateUpdated'].'-'.$filteredarray['monthDateUpdated'].'-'.$filteredarray['dayDateUpdated'].' '.$filteredarray['hourDateUpdated'].':'.$filteredarray['minuteDateUpdated'].':'.$filteredarray['secondDateUpdated'];
    $value=strtotime($time_val);
endif;

并放入array_unique($Values);
在:

and put array_unique($Values);
before the :

$sqlUpdate = "UPDATE $TableName SET ".implode(",",$Values)
                                ." WHERE ID='".intval($PostID)."'";

这篇关于在Foreach循环中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆