onlyican Posted August 1, 2007 Share Posted August 1, 2007 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 https://forums.phpfreaks.com/topic/62817-solved-array-help/ Share on other sites More sharing options...
gerkintrigg Posted August 1, 2007 Share Posted August 1, 2007 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 https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312699 Share on other sites More sharing options...
onlyican Posted August 1, 2007 Author Share Posted August 1, 2007 Sorry, not yet Link to comment https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312704 Share on other sites More sharing options...
clearstatcache Posted August 1, 2007 Share Posted August 1, 2007 foreach($seat as $value) { if ( count(array_keys($seat, $value)) > 1 && !in_array($value, $duplicate) ) $duplicate[] = $value; } Link to comment https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312705 Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 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 https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312706 Share on other sites More sharing options...
jitesh Posted August 1, 2007 Share Posted August 1, 2007 <?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 https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312709 Share on other sites More sharing options...
clearstatcache Posted August 1, 2007 Share Posted August 1, 2007 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 https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312710 Share on other sites More sharing options...
clearstatcache Posted August 1, 2007 Share Posted August 1, 2007 use check w/c gives the best performance...and of course u prefer more... Link to comment https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312711 Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 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 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 https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312717 Share on other sites More sharing options...
onlyican Posted August 1, 2007 Author Share Posted August 1, 2007 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 https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312725 Share on other sites More sharing options...
GingerRobot Posted August 1, 2007 Share Posted August 1, 2007 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 Naa, there is a function that takes an array and removes all duplicates from it: array_unique(), but not one that just gives you the duplicates. Link to comment https://forums.phpfreaks.com/topic/62817-solved-array-help/#findComment-312731 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.