Alicia Posted January 14, 2010 Share Posted January 14, 2010 Hi, Can somebody advise how can I sort the output of $v as we required (numbers ascending and descending) <? $ii=1; $sort_id=array(0,1,2,3,4,5,6,7,8,9); if ($_POST['sort'] == '1D') { // asort($sort_id); } foreach ($sort_id as $v){ $cooo = "SELECT count(number) AS total FROM doc WHERE time BETWEEN $date2 AND $date3"; $com = "$a"."$b"."$c"."$a8"."$d"."$e"."$f"."$g"."$h"."$i"."$j"."$k"; $com3 = "$a8"."$d"."$e"."$f"; $sql = mysql_query($cooo.$com)or die (mysql_error()); while($sd2=mysql_fetch_array($sql)) { ?> <tr valign="top"> <td align="left" bordercolor="#999999" ><? echo "$ii"; ?></td> <td align="left" ><div align="center"><strong><? echo "$v"; ?></strong></div></td> Thanks Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted January 14, 2010 Share Posted January 14, 2010 wouldn't asort work for you? you seem to try to do it in that if statement, but you have it commented out Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted January 14, 2010 Share Posted January 14, 2010 nm I didn't read the post thoroughly Quote Link to comment Share on other sites More sharing options...
Alicia Posted January 14, 2010 Author Share Posted January 14, 2010 hi, no, asort($sort_id); is not working as we are expecting. please advise.. thanks. Quote Link to comment Share on other sites More sharing options...
mikesta707 Posted January 14, 2010 Share Posted January 14, 2010 Well how exactly do you want it sorted? Can you provide an example of an unsorted array, then how you want it sorted? asort will sort it numerically (ascending) You can also get a descending sort with arsort. for example $arr = array(1,2,5,34,2,6,3,2,7,34,6,3,4,6); $arr2 = $arr; asort($arr); arsort($arr2); echo "<pre>"; print_r($arr); echo "</pre>"; echo "<pre>"; print_r($arr2); echo "</pre>"; ?> produces Array ( [0] => 1 [7] => 2 [1] => 2 [4] => 2 [11] => 3 [6] => 3 [12] => 4 [2] => 5 [10] => 6 [5] => 6 [13] => 6 [8] => 7 [9] => 34 [3] => 34 ) Array ( [9] => 34 [3] => 34 [8] => 7 [5] => 6 [13] => 6 [10] => 6 [2] => 5 [12] => 4 [11] => 3 [6] => 3 [4] => 2 [7] => 2 [1] => 2 [0] => 1 ) asort() arsort() if you are working with strings, or have a certain way of sorting in mind, then this might not work, but your first example has an array of numbers. If you don't sort them numerically, how else do you want to sort them.... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted January 14, 2010 Share Posted January 14, 2010 Why don't you just sort it using your query? Quote Link to comment Share on other sites More sharing options...
Alicia Posted January 15, 2010 Author Share Posted January 15, 2010 Hi, because the numbers generated is not from the database so I can't really sort it with query. when we use asort, the numbers will be sorted as below which is not what we are looking for : e.g : 01, 02, 12, 23 then the result will be 10,20,21, 32,,, we want to sort it like 23, 12, 02, 01. Please advise and thanks. Quote Link to comment Share on other sites More sharing options...
taquitosensei Posted January 15, 2010 Share Posted January 15, 2010 use rsort then Quote Link to comment Share on other sites More sharing options...
Alicia Posted January 16, 2010 Author Share Posted January 16, 2010 Hi, I tried this : <?php function permutations($letters,$num){ $last = str_repeat($letters{0},$num); $result = array(); while($last != str_repeat(lastchar($letters),$num)){ $result[] = $last; $last = char_add($letters,$last,$num-1); } $result[] = $last; return $result; } function char_add($digits,$string,$char){ if($string{$char} <> lastchar($digits)){ $string{$char} = $digits{strpos($digits,$string{$char})+1}; return $string; }else{ $string = changeall($string,$digits{0},$char); return char_add($digits,$string,$char-1); } } function lastchar($string){ return $string{strlen($string)-1}; } function changeall($string,$char,$start = 0,$end = 0){ if($end == 0) $end = strlen($string)-1; for($i=$start;$i<=$end;$i++){ $string{$i} = $char; } return $string; } function my_remove($array){ foreach ($array as $k => $v){ $c = str_split($v); sort($c); $c = implode('',$c); if ($v != $c) unset($array[$k]); } return array_values($array); } $number = '1234'; $digit = 2; $Array=permutations($number,$digit); $Array = my_remove($Array); for($i=0 ; $i < count($Array) ; $i++) { echo $Array[$i] . "<BR>\n"; } ?> but the result turned out like this : 11 12 13 14 22 23 24 33 34 44 But what I want is this : 12,13,14,23,24,34. Can some guru advise how to accomplish this. THanks Quote Link to comment Share on other sites More sharing options...
Buddski Posted January 16, 2010 Share Posted January 16, 2010 I think your confusion is confusing everbody.. In your earlier reply you said we want to sort it like 23, 12, 02, 01 but in your last one But what I want is this : 12,13,14,23,24,34. Not to mention but the result turned out like this : 11 12 13 14 22 23 24 33 34 44 But what I want is this : 12,13,14,23,24,34. Are the same... just displayed differently.. Edit: After further reading, im gonna take a stab in the dark here, are you saying that your array values are wrong and that you DONT want 11,22,33 AND 44 in the result? if this is the case array sorting is not the problem.. Quote Link to comment Share on other sites More sharing options...
Alicia Posted January 16, 2010 Author Share Posted January 16, 2010 sorry for the confusion. How can I actually remove the results with the same digits? Please advise. thanks. Quote Link to comment Share on other sites More sharing options...
Alicia Posted January 17, 2010 Author Share Posted January 17, 2010 any guru can help? Quote Link to comment Share on other sites More sharing options...
laffin Posted January 17, 2010 Share Posted January 17, 2010 Uhm, array_unique() Quote Link to comment Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 array_unique wont achieve the result your after. Are all those functions specifically there for turning 1234 into 12,13,14,23,24,34? If so.. There is probably a much easier way to do it.. It helps if we know EXACTLY what your trying to do.. Quote Link to comment Share on other sites More sharing options...
laffin Posted January 17, 2010 Share Posted January 17, 2010 I think I see $nums=array(10,11,20,22,30,33,40,44); echo 'My Nums: '. implode(','$nums) ."<br>"; foreach($nums as $key=>$val) { $remove=true; for($i=1;$i<strlen($val);$i++) { if(substr($val,0,1)!=substr($val,$i,1)) { $remove=false; break; } } if($remove) unset($nums[$key]); } echo 'My Nums: '. implode(','$nums) ."<br>"; Quote Link to comment Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 It started out as sorting but its just gone south from there Quote Link to comment Share on other sites More sharing options...
laffin Posted January 17, 2010 Share Posted January 17, 2010 Missed the commas in implode but it does remove them same sequence numbers My Nums: 10,11,20,22,30,33,40,44 My Nums: 10,20,30,40 Edited code post with implode commas Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.