-2

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>
  • How does the HTML relate to your question? It seems for the essence of your question you only need an array with some values and a desired result (in terms of array). The HTML just adds unnecessary weight to the question. Also, does [this Q&A](https://stackoverflow.com/questions/9229645/remove-duplicate-values-from-js-array) not answer your question? – trincot Mar 30 '19 at 11:14
  • you can use array_unique() method. Kindly check here https://www.php.net/manual/en/function.array-unique.php. – Anshul Mar 30 '19 at 11:17
  • what is the entry point for these day name ? why not you prevent this ? at first step – Niklesh Raut Mar 30 '19 at 11:18
  • Remove all the unrelated stuff from your question, it doesn't add anything – Andreas Mar 30 '19 at 11:20

2 Answers2

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

I Hope this works

Vipertecpro
  • 3,056
  • 2
  • 24
  • 43
0

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.

elissaf
  • 11
  • 3