Do not repeat words within a loop in PHP

0

Each time the loop advances it brings me data for example: light, light, darkness, sun

inside a loop how can I do so that they do not repeat themselves?

foreach($json as $key => $item)
{
//json se ha procesado para que devuelva palabras
echo $item['palabras'];
}

one one two three four four five six

    
asked by Santy SC 15.01.2018 в 16:35
source

1 answer

1

You can use array_unique()

$json = array("verde", "rojo", "verde", "azul", "rojo");
$unicos = array_unique($json);
// verde, rojo, azul

foreach($unicos as $key => $item)
{
    echo $item;
}

You can review the documentation here: link

    
answered by 16.01.2018 в 12:36