简体   繁体   English

如何在不使用另一个数组的情况下删除数组中的重复项?

[英]How can I delete repetitions in an array without using another array?

How can I delete repetitions in an array without using another array?如何在不使用另一个数组的情况下删除数组中的重复项? for example if I write Monday, Wednesday, Monday, Wednesday (in italian: lunedì,mercoledì,lunedì,mercoledi) I need to create a table only with a Monday and a Wednesday例如,如果我写星期一、星期三、星期一、星期三(意大利语:lunedì,mercoledì,lunedì,mercoledi)我需要创建一个只有星期一和星期三的表格

I was thinking of using an if with boolean我正在考虑使用带有布尔值的 if

ps a switch case match one color to each day of the week ps 开关盒为一周中的每一天匹配一种颜色

my code我的代码

 body { color: purple; } table, th, td { border: 1px solid black; width: 44px; } #yellow { background-color: yellow; } #green { background-color: green; } #orange { background-color: orange; } #blue { background-color: blue; } #red { background-color: red; } #purple { background-color: yellowgreen; } #royalblue { background-color: royalblue; }
 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Giorni colorati</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" media="screen" href="giornicolorihtml.css"> <script src="main.js"></script> </head> <body> <h1> Tabella giorni con colori </h1> <form action="giornicolorihtml.php"> <label> Scrivi 7 giorni della settimana: <input type="text" name="giorni" value=""> </label> <br> <br> <br> <br> </form> <?php $frase = $_GET['giorni']; $frasespazi=trim($frase); ?> <table> <tr> <?php $arrayfrase = explode (",", $frase); for ($l=0; $l<count($arrayfrase); $l++) { $confronto = $arrayfrase[$l]; for ($i=count($arrayfrase)-1; $i>0; $i--) { if ($confronto != $arrayfrase[$i]) { switch (true) { case ($arrayfrase[$i] == "lunedì"): $varcolor = "yellow"; break; case ($arrayfrase[$i] == "martedì"): $varcolor = "green"; break; case ($arrayfrase[$i] == "mercoledì"): $varcolor = "orange"; break; case ($arrayfrase[$i] == "giovedì"): $varcolor = "blue"; break; case ($arrayfrase[$i] == "venerdì"): $varcolor = "red"; break; case ($arrayfrase[$i] == "sabato"): $varcolor = "purple"; break; case ($arrayfrase[$i] == "domenica"): $varcolor = "royalblue"; break; } } } ?> <td id="<?php echo $varcolor; ?>"> <?php echo $arrayfrase[$l] ."<br>"; } // for esterno ?> </td> </tr> </table> </body> </html>

$array = array("Monday", "Wednesday", "Monday", "Wednesday");
$array = array_unique($array); // Array is now ("Monday", "Wednesday")

I Hope this works我希望这有效

You could swap the duplicate with the last element in the array, and subtract 1 from its effective length.您可以将副本与数组中的最后一个元素交换,并从其有效长度中减去 1。 if you swap, you don't update the current counter, but you subtract 1 from your "final" index.如果交换,则不会更新当前计数器,而是从“最终”索引中减去 1。 If you don't swap, you update the current counter.如果不交换,则更新当前计数器。 Once your current pointer is greater than final, you're done, and the elements past final are the duplicate.一旦当前指针大于 final,就完成了,并且 final 之后的元素是重复的。

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

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