Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    120
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Duplicates in an Array

    Hi Guys,

    I have an array:
    Code:
    $array = array('test','test2','test3','test3','test3','test4','test4');
    I want to loop through that array and the same time find duplicates in the array and rename them, so I want the end result to be:

    test
    test2
    test3
    test3_2
    test3_4
    test4
    test4_2

    Many Thanks in advance for help.
    Last edited by muneeba9071; 05-31-2011 at 03:30 PM.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    One option.
    PHP Code:
    <?php

    $myArray 
    = array('test''test2''test3''test3''test3''test4''test4');
    foreach (
    $myArray as $val) {
        
    $count 0;
        
    $ext 0;
        for (
    $i 0$i count($myArray); $i++) {
            if (
    $val == $myArray[$i]) {
                ++
    $count;
                if (
    $count 1) {
                    
    $ext += 2;
                    
    $myArray[$i] = $val '_' $ext;
                }
            }
        }
    }
    print_r($myArray);
    ?>

  • Users who have thanked bullant for this post:

    muneeba9071 (05-31-2011)

  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    120
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Many Thanks bullant, that's exactly what I was looking for. Thanks again

  • #4
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    you're welcome

  • #5
    Regular Coder
    Join Date
    Dec 2008
    Posts
    120
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Hi bullant,

    I have another question, what if I have arrays within an array like:

    Code:
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    I want to loop through $array and check it's arrays for duplicates, your above script works perfect for one array, can you please make it work with multiple-arrays.

    Many Thanks

  • #6
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by muneeba9071 View Post
    Hi bullant,

    I have another question, what if I have arrays within an array like:
    You now have a 2D array and so you need an extra foreach loop.
    PHP Code:
    <?php

    $myArray 
    = array();
    $myArray[0] = array('test''test4''test3''test4''test3''test2''test3');
    $myArray[1] = array('test''test3''test4''test3''test4''test3''test2');
    foreach (
    $myArray as $key => $rowArray) {
        foreach (
    $rowArray as $val) {
            
    $count 0;
            
    $ext 0;
            for (
    $i 0$i count($myArray[$key]); $i++) {
                if (
    $val == $myArray[$key][$i]) {
                    ++
    $count;
                    if (
    $count 1) {
                        
    $ext += 2;
                        
    $myArray[$key][$i] = $val '_' $ext;
                    }
                }
            }
        }
    }

    print_r($myArray);
    ?>

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This is actually easier using an array_walk.
    PHP Code:
    function collisionSuffix(&$val$key, &$used)
    {
        if (
    is_string($val))
        {
            if (isset(
    $used[$val]))
            {
                ++
    $used[$val];
            }
            else
            {
                
    $used[$val] = 1;
            }

            if (
    $used[$val] > 1)
            {
                
    $val $val '_' $used[$val];
            }
        }
    }

    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $aUsed = array();
    array_walk_recursive($array'collisionSuffix', &$aUsed);
    printf('<pre>%s</pre>'print_r($arraytrue)); 
    Interesting enough, it only appears to work when the $aUsed is passed by reference at run-time (smells like a bug to me, as $used becomes a scalar value on the second iteration of the recursion). This will suffix all of your variables regardless of which offset they are in. If the intent is to just do each dimension, I'd use a foreach and walk each level through with just an array_walk instead of a recursive one.

  • Users who have thanked Fou-Lu for this post:

    muneeba9071 (05-31-2011)

  • #8
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by muneeba9071 View Post
    I want to loop through $array and check it's arrays for duplicates,.....
    also, you haven't specified if you want to reset the offset counter $ext to 0 at each new row in the 2D array or to just continue incrementing it by 2 from the current value.

    I tossed a coin and it came up as leaving it as the original code.
    Last edited by bullant; 05-31-2011 at 04:03 PM.

  • #9
    Regular Coder
    Join Date
    Dec 2008
    Posts
    120
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Hi Thanks to both of you, I really liked Fou-Lu's solution, Fou-Lu.. is it possible to disable the duplication check on certain array elements?

    Thanks

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by muneeba9071 View Post
    Hi Thanks to both of you, I really liked Fou-Lu's solution, Fou-Lu.. is it possible to disable the duplication check on certain array elements?

    Thanks
    I don't understand what you mean, can you show an example?

  • #11
    Regular Coder
    Join Date
    Dec 2008
    Posts
    120
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Let's say I have an array

    Code:
    array('name' => 'John', 'phone' =>'04578787');
    I want to use your script, but I want to run the duplication on just the phone number

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by muneeba9071 View Post
    Let's say I have an array

    Code:
    array('name' => 'John', 'phone' =>'04578787');
    I want to use your script, but I want to run the duplication on just the phone number
    There are a couple ways.
    The $key within the function will contain the key for the item. You can change the code to accept only specifics for rename:
    PHP Code:
    function collisionSuffixSpecified(&$val$key, &$used)
    {
        if (
    is_string($val))
        {
            if (
    in_array($key$used['specified'], true))
            {
                if (isset(
    $used['used'][$val]))
                {
                    ++
    $used['used'][$val];
                }
                else
                {
                    
    $used['used'][$val] = 1;
                }
                if (
    $used['used'][$val] > 1)
                {
                    
    $val $val '_' $used['used'][$val];
                }
            }
        }
    }
     
    $array[] = array('test''phone' => 'test2''test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3''phone' => 'test2','test3','test4','test4');
    $array[] = array('test','test2','test3','test3''phone' => 'test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');
    $array[] = array('test','test2','test3','test3','test3','test4','test4');

    $aUsed = array(
        
    'specified' => array('phone'), 
        
    'used' => array()
    );
    array_walk_recursive($array'collisionSuffixSpecified', &$aUsed);
    printf('<pre>%s</pre>'print_r($arraytrue)); 
    This does mean that you must use array_walk_recursive to span across all of the dimensions. You cannot otherwise have two keys of the same value within a single array.

  • Users who have thanked Fou-Lu for this post:

    muneeba9071 (05-31-2011)

  • #13
    Regular Coder
    Join Date
    Dec 2008
    Posts
    120
    Thanks
    15
    Thanked 2 Times in 2 Posts
    That's it! Many Thanks!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •