1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

remove duplicate from fields

Discussion in 'PHP' started by mero2020, Jan 11, 2014.

  1. #1
    I have table like this

    id|type
    1|math,science
    2|math,science,math
    3|science,science

    4|math
    5|science
    6|programming,math,math
    7|programming


    i want to remove duplicated to will be like this

    id|type
    1|math,science
    2|math,science
    3|science

    4|math
    5|science
    6|programming,math
    7|programming

    i tried to use array_unique but i can't update table???!
     
    mero2020, Jan 11, 2014 IP
  2. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #2
    build an array and check before adding to the array if the 'key' is already exist.. or 'overwrite' it...

    and programming,math is harder, have no idea about that.
     
    EricBruggema, Jan 12, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    If those are the only values you need to change, just do it from a mysql console (phpmyadmin or similar) and just do "update table set type = 'science' where type = 'science, science'"
     
    PoPSiCLe, Jan 12, 2014 IP
  4. mero2020

    mero2020 Greenhorn

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #4
    No it's many row,this is just example
     
    mero2020, Jan 12, 2014 IP
  5. Code Developer

    Code Developer Active Member

    Messages:
    48
    Likes Received:
    2
    Best Answers:
    2
    Trophy Points:
    58
    #5
    Something like this would do the job:

    function unique($string) {
        return implode(',', array_keys(array_flip(explode(',', $string))));
    }
    PHP:
    Usage :
    $values = "programming,math,math";  //Output : programming,math
    echo unique($values);
    PHP:
     
    Code Developer, Jan 12, 2014 IP
  6. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #6
    Why can't you? If you don't have UPDATE permissions on your MySQL user, then even stored procedure won't help you here. array_unique should work perfectly, maybe if you show us your code we can let you know if there is a mistake in it.
     
    ThePHPMaster, Jan 12, 2014 IP
  7. Marcel Preda

    Marcel Preda Member

    Messages:
    21
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    43
    #7
    just tested the code from Code Developer, and it seems to be what you want. you just need to code couple of lines.
     
    Marcel Preda, Jan 17, 2014 IP