Jump to content

sort values in for loop


Alicia

Recommended Posts

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/188494-sort-values-in-for-loop/
Share on other sites

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....

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.

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

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..

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..

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>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.