删除Foreach循环中的重复项

问题描述 投票:0回答:3

我看到其他问题看起来像这个但实际上却完全不同,所以我发布了自己的版本。下面的代码,特别是表格,已经过简化,以便更好地适应。

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

UPDATE tablename SET 
`Name`='First Last',
`EMail`='[email protected]',
`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 protected]" 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>
php post foreach
3个回答
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
投票
$my_Unique_Array = array_unique($my_Array);

0
投票
$check_point='';
foreach ()
{

    if(!in_array($check_point, $array))
    {

     //condition
     // do your whole updating task here and after this assign it to a variable  for checking next time , so if it's found then unset it .

      $check_point=$array[$va];
     }
     else{

         unset($array[$check_point]);
         $check_point='';
     }
}
© www.soinside.com 2019 - 2024. All rights reserved.