SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Delete duplicate array completely?

    Yo,

    I'm trying to compare and remove duplicate element (value) from two different arrays.
    Code:
     	//Call jaz_remove_duplicate_array
    	function rda() {
    		$ar1 = array(1, 2, 3, 4, 5, 6, 7); //Original array
    		$ar2 = array(2, 4, 8, 1, 9, 7); //Result array
    		$this->jaz_remove_duplicate_array($ar1, $ar2);
    	}
     
     	//Merge array and remove duplicate
    	function jaz_remove_duplicate_array($ar1, $ar2) {
    		echo 'Array 1<br>';
    		foreach($ar1 as $key => $value){
    			echo $key . '-' . $value .'<br>';
    		}
    		
    		echo '<br>';
    		echo 'Array 2<br>';
    		foreach($ar2 as $key => $value){
    			echo $key . '-' . $value .'<br>';
    		}
    
    		//check if base array is empty.
    		if(count($ar1) < 1) {
    			return FALSE;	
    		}	
    		
    		//Else continue removing value if duplicate value found.
    		reset($ar2);
    		echo '<br>Looping: <br>';
    			foreach($ar2 as $key2 => $value2) {
    				if (in_array($value2, $ar1)) {
    				    //If duplicate found, delete duplicate on base array (array 1).
    				    unset($ar1[$value2]);
    				}
    			}
    		//echo $value1 .'<br>';
    		
    		echo '<br>Result: <br>';
    		print_r($ar1);		
    	}
    I tried array_unique(), but not the exact method I'm looking.

    I tried googling this but the solution I found needs higher version of PHP 5.4.
    I need a solution that will work under PHP 5.2.
    My codes above it seems to work, but I guess it needs more tweaking.

    For the example above it suppose to output 8 and 9 since these numbers has no duplicate.

    Thanks in advance.
    Quality codes are optimized and tested...
    Click here for inspiration..

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,068
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php

    $ar1 
    = array(1234567); //Original array
    $ar2 = array(248197); //Result array

    $duplicates=array();
    $non_duplicates=array();

    foreach (
    $ar2 AS $value ) {
        if (
    in_array($value,$ar1)) {
            
    array_push($duplicates,$value);
        } else {
            
    array_push($non_duplicates,$value);
        }
    }
    var_dump($non_duplicates);
    ?>
    I belive that array_push() is available in php version 5.2 but can't be sure
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @SpacePhoenix ;
    WoW, what a nice trick.
    I did not know array_push() can do that.

    Thanks a lot man.
    Quality codes are optimized and tested...
    Click here for inspiration..

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Code:
    $ar3 =array_diff($ar2, $ar1);
    Gives:
    8
    9

    Note though that your function also checks for at least one array not to be empty, so if that is the spec you'd still have to do that check.

  5. #5
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    @Cups ;

    Cool, the shorter the better.
    Thanks.
    Quality codes are optimized and tested...
    Click here for inspiration..


Bookmarks

Posting Permissions

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