shedokan Posted February 8, 2008 Share Posted February 8, 2008 I have this array: <?php $array=array(array(1,2,3,4),array(1,2,4,3),array(6,2,4,4),array(6,4,2,4),array(6,5,4,1),array(6,4,1,5) ); ?> is there a way to filter so there won't be arrays that are almost the same like: 1,2,3,4 is equal to 1,2,4,3 6,2,4,4 is equal to 6,4,2,4 6,5,4,1 is equal to 6,4,1,5 please, thanks. Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/ Share on other sites More sharing options...
BlueSkyIS Posted February 9, 2008 Share Posted February 9, 2008 this may not be perfect: $array=array(array(1,2,3,4),array(1,2,4,3),array(6,2,4,4),array(6,4,2,4),array(6,5,4,1),array(6,4,1,5)); for ($i=0;$i<count($array);$i++) { sort($array[$i]); } $current_array = ""; $unique_arrays = array(); foreach($array AS $an_array) { $last_different = FALSE; if ($current_array === "") { $current_array = $an_array; } else { // Compare an_array with current_array for($j=0;$j<count($current_array);$j++) { if ($current_array[$j] != $an_array[$j]) { $unique_arrays[] = $current_array; $current_array = $an_array; $last_different = TRUE; break; } } } } // Don't forget the last an_array $unique_arrays[] = $an_array; echo"<P>UNIQUE:</P>"; print_r($unique_arrays); Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462260 Share on other sites More sharing options...
Barand Posted February 9, 2008 Share Posted February 9, 2008 try <?php $array = array( array(1,2,3,4), array(1,2,4,3), array(6,2,4,4), array(6,4,2,4), array(6,5,4,1), array(6,4,1,5) ); $x = array(); $filtered = array(); foreach ($array as $a) { if (array_intersect($a, $x) != $a) $filtered[] = $a; $x = $a; } // result echo '<pre>', print_r($filtered, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462267 Share on other sites More sharing options...
phpSensei Posted February 9, 2008 Share Posted February 9, 2008 try <?php $array = array( array(1,2,3,4), array(1,2,4,3), array(6,2,4,4), array(6,4,2,4), array(6,5,4,1), array(6,4,1,5) ); $x = array(); $filtered = array(); foreach ($array as $a) { if (array_intersect($a, $x) != $a) $filtered[] = $a; $x = $a; } // result echo '<pre>', print_r($filtered, true), '</pre>'; ?> I was writing an entire script and this came up. Lol Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462274 Share on other sites More sharing options...
Barand Posted February 9, 2008 Share Posted February 9, 2008 It does depend on the array being conveniently sequenced. If they are jumbled then would require sorting first eg <?php $array = array( array(1,2,3,4), array(6,5,4,1), array(6,2,4,4), array(6,4,2,4), array(1,2,4,3), array(6,4,1,5) ); foreach ($array as $k=>$a) { sort($a); $array[$k] = $a; } array_multisort($array); $x = array(); $filtered = array(); foreach ($array as $a) { if (array_intersect($a, $x) != $a) $filtered[] = $a; $x = $a; } echo '<pre>', print_r($filtered, true), '</pre>'; ?> Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462280 Share on other sites More sharing options...
shedokan Posted February 9, 2008 Author Share Posted February 9, 2008 is there a way to: filter: 1,2,4,3 and leave 1,2,3,4 but not filter: 6,5,4,1 with 1,4,5,6 thanks. Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462472 Share on other sites More sharing options...
trq Posted February 9, 2008 Share Posted February 9, 2008 Yes there are ways. I would suggest looking at the various array functions and seeing what you come up with. Post your code if you get stuck. Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462477 Share on other sites More sharing options...
shedokan Posted February 9, 2008 Author Share Posted February 9, 2008 but I understand those functions like I'm bill gates. Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462523 Share on other sites More sharing options...
Barand Posted February 9, 2008 Share Posted February 9, 2008 And that will always be the case unless you try. Read the manual, play around with them, experiment. See what they do. Did you think everyone else was born knowing how they all work? If you get really stuck, post back. Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462526 Share on other sites More sharing options...
shedokan Posted February 9, 2008 Author Share Posted February 9, 2008 I'll try to weith a different way. I'll filter groups of 36 seperatly using explode. if I'll have any problems I will post back. Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462527 Share on other sites More sharing options...
shedokan Posted February 9, 2008 Author Share Posted February 9, 2008 help please, it doesn't filters all here's my code: <?php $code=<<<END 3+2+4+7 3+1+1+5 3+1+5+1 3+2+7+4 3+4+2+7 END; $code = explode(" ", $code); $count=count($code); for($i=0;$i<=$count;$i++) $code[$i] = explode("+", $code[$i]); foreach ($code as $k=>$a) { sort($a); $code[$k] = $a; } for($i=1;$i<=2;$i++){ $x = array(); $filtered = array(); foreach ($code as $a) { if (array_intersect($a, $x) != $a) $filtered[] = $a; $x = $a; } $code=$filtered; } $count=count($code)-1; for($i=0;$i<=$count;$i++){ $d=$code[$i][0]; $c=$code[$i][1]; $b=$code[$i][2]; $a=$code[$i][3]; if($a != '' || $b != '' || $c != '' || $d != '') $final .=$a.'+'.$b.'+'.$c.'+'.$d.'='."\n"; } echo $final; ?> Link to comment https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/#findComment-462562 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.