简体   繁体   中英

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

I was thinking of using an if with boolean

ps a switch case match one color to each day of the week

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. if you swap, you don't update the current counter, but you subtract 1 from your "final" index. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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