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 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts

    get duplicate values from an array and reuse

    Hi
    Can anyone help with getting any duplicate values out of an array and build another array out of them?

    Any pointers would be really helpful, I have tried array unique and a few others but cannot identify the dupes and get them out.
    cheers ;-)

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    PHP Code:
    function array_duplicates($array)
    {
        if(!
    is_array($array))
            return 
    false;
        
    $duplicates = array();
        
    $unique array_unique($array);
        if(
    count($array) > count($unique))
            for(
    $i 0$i count($array); $i++)
                if(!
    array_key_exists($i$unique))
                    
    $duplicates[] = $array[$i];
        return 
    $duplicates;
    }

    $a = array('1'1234'4'5'5');
    $duplicates array_duplicates($a);
    $a array_unique($a); 

  • #3
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
     $newarray array_unique(array_diff($arrayarray_unique($array))); 

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by Mwnciau View Post
    PHP Code:
     $newarray array_unique(array_diff($arrayarray_unique($array))); 
    Did you try that code?

  • #5
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    Nope, I didn't realise that array_diff didn't find the difference including duplicates.

  • #6
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks guys giving it a go now.... ;-)

    OK so far..........
    The basis of this function works just fine.
    New problem is to get all duplicates out as individual values so that they can be used to update individually.

    I have managed to use the above function to some extent to loop ( I use that term rather loosely) through the array in order to test to see if there are any further unique values and extract them also.

    The problem is that there could ba any number of duplicates in the array and I don't really want to write lines and lines of code to keep extracting.
    There must be an easier way than the one I have got .
    Lets say that the user inputs 10 values, each one is needed to update db fields so duplicates won't allow this.
    If the user has 9 dupes then each of the 9 has to be extracted somehow in order to run the update.
    Code:
    $prod = $_REQUEST[stock_in];//input from form 
    
    $wordChunks = explode("\r\n", $prod);//explode at the end of item name / 
    trim($wordChunks);
    ///////////////////////////////////////////function for duplicates
    function array_duplicates($array)
    {
        if(!is_array($array))
            return false;
        $duplicates = array();
        $unique = array_unique($array);
        if(count($array) > count($unique))
            for($i = 0; $i < count($array); $i++)
                if(!array_key_exists($i, $unique))
                    $duplicates[] = $array[$i];
        return $duplicates;
    }
    
    $a = $wordChunks;
    $duplicates = array_duplicates($a);
    $a3 = array_unique($a);
    
    $wordChunksD = implode("','", $duplicates);
    echo "dupes = ".$wordChunksD."<br>";
    
    echo"==============<br>";
    
    $wordChunks2 = implode("','", $a3);
    echo "unique = ".$wordChunks2."<br>";
    
    echo"==============<br>";
    //////////////////////////////// second time around//////////////////////////
    
    $wordChunksE = explode("','", $wordChunksD);//explode at the end of item name / 
    trim($wordChunksE);
    $ab = $wordChunksE;
    $ax = array_unique($ab);
    $duplicates2 = array_duplicates($ab);
    
    $wordChunksE = implode("','", $duplicates2);
    echo "next dupes = ".$wordChunksE."<br>";
    
    echo"==============<br>";
    $wordChunksF = implode("','", $ax);
    echo "next unique = ".$wordChunksF."<br>";
    Is it possible to do this from within a loop?
    i.e. check for dupes, extract the unique ones, update them, check for dupes in the new array, extract them and update the unique ones and so on until all have been updated???

    Hope you can help me.
    Cheers
    Last edited by clunky; 08-29-2007 at 05:07 PM.

  • #7
    New Coder
    Join Date
    Jul 2007
    Posts
    65
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Anyone please?

  • #8
    Regular Coder
    Join Date
    May 2006
    Location
    Wales
    Posts
    820
    Thanks
    1
    Thanked 82 Times in 79 Posts
    PHP Code:
    function getdupes($array){
      if (!
    is_array($array)) return false;
        
    $found = array();
        foreach(
    $array as $key => $value){
          if (!
    in_array($value$found)){
            unset(
    $array[$key]);
            
    $found[] = $value;
         }
      }
    return 
    $array;
    }
    function 
    hasdupes($array){
      if (!
    is_array($array)) return false;
        
    $found = array();
        foreach(
    $array as $key => $value){
          if (!
    in_array($value$found)){
            unset(
    $array[$key]);
            
    $found[] = $value;
         } else {
           return 
    true;
         }
      }
      return 
    false;
    }

    $array = array( ... );

    while (
    hasdupes($array)){
    $array getdupes($array);
    $dupes array_unique($array);

    // $dupes contains all the dupes once only




  •  

    Posting Permissions

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