Transform string into array excluding repeated values

Asked

Viewed 54 times

0

In PHP I made a script that takes each word of a Tring and turns them into an array, giving explodes into empty spaces. Then he eliminates the repeated words and shows each of them, like this:

<?php
$hashtag = "#caio #azul #caio #azul #leão #orelha #caio  #caio  #caio";
//
$hashtagArray = explode(" ", $hashtag);
// exclui elementos vazios
$hashtagArray = array_filter($hashtagArray);
// exclui elementos iguais
$hashtagArray = array_unique($hashtagArray);
// reseta as chaves
$hashtagArray = array_values($hashtagArray);
// cria uma array
$hashtagArrayDuplica = array();
foreach ($hashtagArray as &$value) {
    // repito a checagem, para evitar erros
    if (!in_array($value, $hashtagArrayDuplica)) {
        $hashtagArrayDuplica[] = $value;
        echo $value."<br>";
    }
}
?>

The result obtained is this:

#caio
#azul
#leão
#orelha

Which seems to be right. But incredibly on the client server, this application keeps showing the repeated items, practically disregarding my code, I don’t know if it’s for the PHP version, or any other reason (I can’t see the script running there, I only get print screen)... what can it be? Can there be any special character? Is there another way to do this check, to see if the server works?

PS: Notice that in my code I try to check 2 times if you have something repeated, it was an attempt not to give error in the client, but without success.

  • Ask the client to print the phpinfo()

  • Only explode, array_filter and array_unique who has in question do what you want. See here. The problem is certainly not of the version as these 3 functions are supported from PHP4 (you can confirm in the documentation of each). I suspect the problem is another.

  • I checked and there is running version 5.6

  • Maybe some way to clear every word by taking out any different character?

  • The problem shouldn’t be in that code, it’s something else. I suggest starting by activating all the warnings to try to see any other problems that might exist. And you can test each function individually on a new test page step by step until you realize where the problem is.

  • You can test your code in different versions of PHP at this link http://sandbox.onlinephpfunctions.com/code/04bd3a78eabf54735755d284c766c8a54fdc1318

  • Nothing wrong http://sandbox.onlinephpfunctions.com/code/eb9ef3f04f2f82b00776d14263cfac68a1883759

  • on another site https://ideone.com/AnF7Ik

  • Valeu @Leocaracciolo did not know these sites, very useful!

Show 4 more comments

1 answer

0


I could test like this here ?

$hashtag = "#caio #azul #caio #azul #leão #orelha #caio  #caio  #caio";
//
$hashtagArray = explode(" ", $hashtag);

echo "<pre>";
print_r($hashtagArray);
echo "</pre>";

foreach ($hashtagArray as $key => $value){
  if (strlen($value)>0){
    $new[trim($value)] = $value;
  }
}

echo "<pre>";
print_r($new);
echo "</pre>";

exit();

Another question ... has this code ever been wrong before? It may be that they are not putting your file in the right place on the server, then you could never know what is wrong, because you would be running 1 old code...

  • They are putting it in the right place yes, because other functions in that same file work well. I just made a version with your code, and I sent it, let’s see! Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.