php - Output the array such that -
i making program output array in ascending order if there found matching number skip , consider later. example: array = 2, 1, 3, 1, 5, 2; output should be: array = 1, 2, 3, 5, 1, 2 //first 4 sequence number(1,2,3,5) , repeating number sequenced later.
here code
<?php if(isset($_post['submit'])) { $var = $_post['in']; $arr = explode(" ",$var); sort($arr); $size = sizeof($arr); $arr2 = array(); $cnt=0; $k=0; for($i=0;$i<$size;$i++) { for($j=0;$j<$size;$j++) { $k = $j + 1; if($arr[$j] < $arr[$k]) { $arr2[$j] = $arr[$j]; array_splice($arr,$j,1); } if($arr[$j] == $arr[$k]) { continue; $cnt++; } if($cnt==0) { break; } } } foreach($arr2 $value) { echo " ".$value; } } ?> <html> <head></head> <body> <form method="post"> <h2>enter data</h2> <input type="text" name="in" placeholder="enter data spaces.."/> <input type="submit" value="ok" name="submit"/> </form> </body> </html>
it not working. please correct code.
thanks
try this:
$arr = array( 2, 1, 3, 1, 5, 2,2); $groups = array(array()); foreach($arr $a) { $found = false; foreach($groups &$g) { if(in_array($a, $g)) { continue; } $g[] = $a; $found = true; break; } if(!$found) { $groups[] = array($a); } } foreach($groups &$g) { sort($g); } $result = array(); foreach($groups &$g) { $result = array_merge($result, $g); } var_dump($result);
it work unlimited duplicates
Comments
Post a Comment