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,650
    Likes Received:
    22
    Best Answers:
    12
    Trophy Points:
    115
    #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 Well-Known Member

    Messages:
    1,224
    Likes Received:
    109
    Best Answers:
    51
    Trophy Points:
    160
    #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:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #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:

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

    ThePHPMaster Well-Known Member

    Messages:
    595
    Likes Received:
    30
    Best Answers:
    18
    Trophy Points:
    100
    #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:
    20
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    33
    #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