1

I can not "remove" the double values ​​from an array even if I use the function array_unique!

<?php
    $tags_array = array() ; 
    $query_tags = $mysql->Query("SELECT words AS kw FROM users ") ;
    /****
    *
    * This query return something like Array([1] => PHP,ASP,NET [2] => Ruby,Jquery,php,asp_net [3] => Php,asp,visualbasic,c# [4] => [5] =>)
    *
    *****/
    while($fetch_tags = $mysql->Fetch($query_tags)) 
    {
        foreach(explode(',',$fetch_tags['kw']) as $kw => $value) 
        {
            if(empty($value)) ;
            else 
            {
                $value = ucwords(strtolower($value)) ;
                $tags_array[] = $value ;
            } 
        }
    }
$tags_array = array_values(array_unique($tags_array, SORT_STRING)) ; 
print_r($tags_array) ;
/******
*
* print_r show somethings like this Array([1] => Asp [2] => Php [3] => Php [4] => Ruby [5] => Jquery [6] => Php [7] => Asp_net [8] = >C# [9] => Asp) 
*
* IT'S ONLY AN EXAMPLE TO SHOW YOU THE SITUATION 
*****/
?>
4
  • Sorry i'm little noob tell me who i can do it :( apologize me Commented Jul 21, 2011 at 16:07
  • Maybe there is some whitespace involved? E.g., "PHP, Ruby", "Asp, PHP" -> array("PHP", " Ruby", "Asp", " PHP"). Commented Jul 21, 2011 at 16:07
  • I mean $value = ucwords(strtolower(trim($value)));. If the values have white spaces at the beginning or at the end, they won't be equal... Commented Jul 21, 2011 at 16:16
  • yes there is some whitespace ! damn i'm very stupid TNX Felix AND Ferdinand !!!!!!!!!!!!!! Commented Jul 21, 2011 at 16:17

4 Answers 4

1

Make sure that the returned values are in fact not unique. For example

$foo = array("PHP","php","pHP","PHP ");
$foo = array_unique($foo);

Will still contain 4 entries.

If any entries contain spaces you should trim these.

Sign up to request clarification or add additional context in comments.

2 Comments

This is why he normalizes those with ucwords(strtolower($value))?
@Ferdinand. The 4th entry has a space which will be missed by that.
0

Just use values as keys, they can only exist once and you don't have any numbers as your keywords (hopefully):

$tags_array = array_keys(array_flip(($tags_array));

array_flip will use values as keys (and drop duplicate values) and array_keys will then return all keys as values again.

5 Comments

Why should he use array_keys(array_flip(...)) instead of just using array_unique()?
I agree with Ferdinand, seems convoluted at best, and breaks when the values aren't scalars.
@Wrikken: In the question the values all are strings, no numeric parts given.
@Ferdinand Beyer: As an example to show that array_unique does work.
@hakre: yes, in this question they are. However, people look at the answers here online more then they ask, and others may have non-scalars in their arrays. Suggesting a possibly broken solution for them (granted, in this scenario it will work although it didn't solve the real problem) while there is a proper tool in the question (namely array_unique) will not serve them well. I mainly commented for their sake, outlining when and where this solution is not valid. Your solution isn't wrong, or I would've downvoted, it's just not the proper tool.
0

Given that is the only thing that aray_unique is supposed to do, I find it very surprising it's not doing it. What is apparent from your post, is that maybe you think 'php' is the same thing as 'PHP'?

When I try the following I get unique results:

$d=Array('PHP,ASP,NET','Ruby,Jquery,php,asp_net','Php,asp,visualbasic,c#');

$o=array();

foreach ($d as $i) {
    $p=explode(',',$i);
    foreach ($p as $q) {
      $o[]=strtoupper($q);
    }
}
print_r(array_unique($o));

However the issue only arises because your database schema is not normalised.

Comments

0

As no one seemed to have provided the right answer, I'll repeat my comment here:

It might be that the words have preceding or trailing white spaces. Then they will never be equal to each other. You can remove these white spaces with trim [docs]

$value = ucwords(strtolower(trim($value)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.