array question..

Discussion in 'PHP' started by lowridertj, Jun 19, 2009.

  1. #1
    If I have an array

    DGN, DGN, TEST, TEST3,TEST3, TEST4, TEST4

    and this is being transferred to a text box.

    How can I remove the duplicate entries?

    Example code:

    
    //While the URL is passing the cc string
    // DGN, DGN, TEST, TEST3,TEST3, TEST4, TEST4
    
    $cc = $_GET['cc'];
    
    
    PHP:
    
    
    <input name="ccc" type="text" value="<?php echo "$cc";?>"/>
    
    
    
    HTML:
    Any and all help with this would be greatly appreciated. I been racking my head around and think I have been over thinking this for to long that I cant figure it out.

    TJ
     
    lowridertj, Jun 19, 2009 IP
  2. webhed21

    webhed21 Member

    Messages:
    55
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    use the array_unique($array) function which is built into php. It removes all duplicate values from an array.

    
    $cc = array_unique($cc);
    
    PHP:
     
    webhed21, Jun 19, 2009 IP
  3. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #3
    tried it.

    
    //While the URL is passing the cc string
    // DGN, DGN, TEST, TEST3,TEST3, TEST4, TEST4
    
    $cc = "DGN, DGN, TEST, TEST3,TEST3, TEST4, TEST4";
    print_r($cc);
    echo "<BR>";
    $cc = trim($cc);
    print_r($cc);
    echo "<BR>";
    $c = explode(" , ", $cc);
    print_r($cc);
    echo "<BR>";
    $cc = array_unique($cc);
    print_r($cc);
    
    
    PHP:

    doesnt work.
     
    lowridertj, Jun 19, 2009 IP
  4. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #4
    It does work. The real issue here is that you are not using descriptive variable names which makes debugging code a nightmare. Try this:

    <pre><?php
    
    $myString = "DGN,DGN,TEST,TEST3,TEST3,TEST4,TEST4";
    print_r($myString);
    echo "<BR>";
    $myString = trim($myString);
    print_r($myString);
    echo "<BR>";
    $myArray = explode(",", $myString);
    print_r($myArray);
    echo "<BR>";
    $myArray = array_unique($myArray);
    print_r($myArray);
    
    ?></pre>
    PHP:
    Brew
     
    Brewster, Jun 19, 2009 IP
  5. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #5
    thank you.

    Now while it shows
    
    Array
    (
        [0] => DGN
        [1] => DGN
        [2] => TEST
        [3] => TEST3
        [4] => TEST3
        [5] => TEST4
        [6] => TEST4
    )
    
    Array
    (
        [0] => DGN
        [2] => TEST
        [3] => TEST3
        [5] => TEST4
    )
    
    
    Code (markup):
    How can I get the second set to display together as
    DGN, TEST, TEST3, TEST4

    shown as $cc = DGN, TEST, TEST3, TEST4;

    to be able to display it in the form text area?


    And again I do appreciate all the help guys and gals.

    TJ
     
    lowridertj, Jun 19, 2009 IP
  6. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #6
    I love easy questions :) Use the function implode()

    implode(', ', $myArray)
    PHP:
    Brew
     
    Brewster, Jun 19, 2009 IP
  7. lowridertj

    lowridertj Well-Known Member

    Messages:
    2,882
    Likes Received:
    40
    Best Answers:
    0
    Trophy Points:
    195
    #7
    much appreciated..
    .
    TJ
     
    lowridertj, Jun 19, 2009 IP