在Foreach循环中删除重复项

时间:2018-12-06 09:06:54

标签: php post foreach

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

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

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值!

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;

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

<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>

3 个答案:

答案 0 :(得分:1)

删除此内容:

// 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);
在:

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

答案 1 :(得分:1)

$my_Unique_Array = array_unique($my_Array);

答案 2 :(得分:0)

ng test --source-map