Jump to content

[SOLVED] Array Help


onlyican

Recommended Posts

Hey people

I am building a Seating plan editor.

Ignore the Keys as it is not important for this question.

I have an Array such as

[J/1] => J1 
[J/2] => J2 
[J/3] => J3 
[J/4] => J1 
[J/5] => J3 
[J/6] => J6 
[J/7] => J7);

Warning: Original array could be up to 1000 values.

 

What I need to do is get an array of all of the values which are Duplicated.

For example, the above, I would like an array of

Array ([0] => "J1", [1] => "J3");

As there are more than 1 entries for J1 and J3 as vaulues, (NOT Keys, Ignore Keys, just Values).

 

What is the best way of doing this, Remember that the array could contain 1000 values. maybe more, maybe less.

 

Link to comment
Share on other sites

I'd probably do something like use a foreach() loop to get the value of each and then do a test on each of them...

so something like:

 

if ($value is in one of the existing variables){
increment the appropriate variable value by 1;
}
else{
create a new variable and set it's value to 1;
}

 

Pseudo code only.... I have no idea HOW to do this.

Link to comment
Share on other sites

I couldn't get yours to work clearstatcache(which ive just noticed was my fault, whoops!), so here's mine. Looks like more code, but in a topic a while back, Barand showed that it was faster than using in_array().

 

 

<?php
$array = array('J/1' => 'J1'
,'J/2' => 'J2' 
,'J/3' => 'J3' 
,'J/4' => 'J1' 
,'J/5' => 'J3' 
,'J/6' => 'J6' 
,'J/7' => 'J7');
sort($array);//re-index array with numeric keys
$n = count($array);
for($x=0;$x<$n;$x++){
   for($y=$x+1;$y<$n;$y++){
       if($array[$x] == $array[$y]){
           echo 'Duplicate value:'.$array[$x].'<br />';
       }        
   }
}
?>

Link to comment
Share on other sites

<?php

$array_o = array("a","b","c","d","a","a","b");

$dup_arr = array();
$uni_arr = array();
foreach($array_o as $key => $value){
       if(!in_array($value,$uni_arr)){
   		$uni_arr[] = $value;
   }else{
   		$dup_arr[] = $value;
   }
}
$dup_arr = array_unique($dup_arr);
echo "<pre>";
print_r($dup_arr);
?>

Link to comment
Share on other sites

here's d complete code GingerRobot

 

<?php

 

$array = array('J/1' => 'J1'

,'J/2' => 'J2'

,'J/3' => 'J3'

,'J/4' => 'J1'

,'J/5' => 'J3'

,'J/6' => 'J6'

,'J/7' => 'J7');

 

$duplicate = array();

foreach($array as $value) {

    if ( count(array_keys($array, $value)) > 1 && !in_array($value, $duplicate) )

        $duplicate[] = $value;

}

print_r($duplicate);

 

Link to comment
Share on other sites

Well, if you're interested:

 

<?php
$array = array('J/1' => 'J1'
,'J/2' => 'J2' 
,'J/3' => 'J3' 
,'J/4' => 'J1' 
,'J/5' => 'J3' 
,'J/6' => 'J6' 
,'J/7' => 'J7');
$duplicate = array();
$t1 = microtime(true);//start timer
for($i=0;$i<=10000;$i++){
    $duplicate = array();
    sort($array);//re-index array with numeric keys
    $n = count($array);
    for($x=0;$x<$n;$x++){
        for($y=$x+1;$y<$n;$y++){
            if($array[$x] == $array[$y]){
                $duplicate[] = $array[$x];
            }        
        }
    }
}
$t2 = microtime(true);//intermediate time
for($i=0;$i<=10000;$i++){
    $duplicate = array();
    foreach($array as $value) {
        if ( count(array_keys($array, $value)) > 1 && !in_array($value, $duplicate) )
            $duplicate[] = $value;
    }
}
$t3 = microtime(true);//end time

printf ("1st Method: %0.8f<br>2nd Method: %0.8f<br>Ratio (1st method/2nd method): %0.2f", $t2-$t1, $t3-$t2, ($t2-$t1)/($t3-$t2));

 

Results:

 

1st Method: 0.38158202
2nd Method: 0.52205086
Ratio (1st method/2nd method): 0.73

 

Of course, even over 10000 runs of each, there's still only aroun 0.1 of a second differance, so its not exactly all that important. Id probably use yours, for the less typing :P

 

Lastly, if the array already had numerical indexes and there was no need to sort, then the differance in speed between mine and yours is more noticeable

Link to comment
Share on other sites

OK

 

I did think there was a function in PHP which returned duplicated fields but I can't find one, so with my knowledge and your help, I built one

 

<?php
function GetDuplicatedValues($Array){

$DuplicatedValues 	= array();
//First check if function is needed
$UniqueArray = array_unique($Array);
if($Array != $ArrayUnique){
	//Loop through the Array
	foreach($Array as $Value){
		//Check if more than 1 Key is found for the value, Means Duplicate found
		if(count(array_key($Array, $Value)) > 1){
			//Add to new array
			$DuplicatedValues[] = $Value;
		}
	}
}
//Clear out duplicates
$DuplicatedValues = array_unique($DuplicatedValues);
return $DuplicatedValues;

}


$array = array('J/1' => 'J1'
,'J/2' => 'J2' 
,'J/3' => 'J3' 
,'J/4' => 'J1' 
,'J/5' => 'J3' 
,'J/6' => 'J6' 
,'J/7' => 'J7');

$DuplicateEntries = GetDuplicatedValues($array);


print_r($DuplicateEntires);

?>

 

$DuplicateEntires should return

 

[0] = J1

[1] = J3

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.