<?php
function cmp($a, $b)
{
  if ($a == $b) {
    return 0;
  }
  return ($a < $b) ? -1 : 1;
}

$a = array(3, 2, 5, 6, 1);

usort($a, "cmp");

while (list($key, $value) = each($a)) {
  echo "$key: $value\n";
}
?>

i've read this example from some php tutoria,but i don't really understand what's does -1 :1 means? Can anyone please kindly help?thank you so very much.

    return ($a < $b) ? -1 : 1;

    This means that if ($a < $b) evaluates as TRUE, then return -1, else return 1. (See the ternary operator for more info.)

      I believe there is a simpler way to implement cmp() for integers:

      function cmp($a, $b)
      {
          return $a - $b;
      }

      It is not terribly useful in this case as just using sort() will do, but a variant of this technique can be useful for say, sorting a multi-dimensional array or an array of objects based on some numeric member data.

        NogDog wrote:
        return ($a < $b) ? -1 : 1; 

        Something I should know but I don't is whether not returning 0 if they're equal could cause a problem in the sort ("4 is greater than 4! NO! I mean the other way around: 4 is greater than 4!")

        Quicksort isn't a stable algorithm, so I suspect it's not an issue - if memory serves the algorithm doesn't actually need to recognise equality between elements. So it would be a matter of some unnecessary comparisons/swaps/splits. I don't think there's the potential for an infinite loop.

          Quicksort isn't a stable algorithm, so I suspect it's not an issue - if memory serves the algorithm doesn't actually need to recognise equality between elements. So it would be a matter of some unnecessary comparisons/swaps/splits. I don't think there's the potential for an infinite loop.

          The sort will definitely terminate, and stable sorts should still work correctly. In fact, the C++ standard library's sort() and stable_sort() function templates will work for any type that has operator < defined, even if there is no operator == defined. From what I know of the common sorting algorithms, there is pretty much no need to compare for equality.

            <form action="Untitled-1.php" method="post">
            <input name="bil" type="text" />
            <input name="form" type="submit" value="Generate Form" />
            <?
            $bil = isset($_POST[bil]) ? $_POST[bil] : '';
            
            if(is_numeric($bil))
            {
            if(isset($_POST['form']))
            {
            ?>
            <table width="200" border="1">
            </p>
              <tr>
                <td>Task</td>
                <td>Task Time(in min)</td>
              </tr>
            
            <?
            $num = 1;
            while($num<=$bil)
            {
            ?>
            
            <tr>
            <td><input type="text" name="biltugas[]" size="15" /></td>
            <td><input type="text" name="masatugas[]" size="15" /></td>
            </tr>
            </form>
            <?
            $num++;
            }
            echo "</table>";
            }
            }
            else{
            ?>
            <script language="JavaScript">
            alert("please enter in numeric");
            </script>
            <?
            }
            ?> 
            

            This is my untitled-1.php page,this coding will be generate the numbers of tables when users input how many tables they would want.

            <?php
            $biltugas = isset($_POST[biltugas]) ? $_POST[biltugas]:'';
            $masatugas = isset($_POST[masatugas]) ? $_POST[masatugas ]:'';
            
            function kiramasatugas($masatugas)
            {
            if(is_array($masatugas))
            {
            $count=0;
            foreach($masatugas as $key=>$value)
            {
            if(is_numeric($value))
            {
            $MSTG[$count] = $value;
            }
            else
            {
            $MSTG[$count] = 0;
            }
            $count++;
            }
            return $MSTG;
            }
            return false;
            }
            asort($masatugas);
            asort($biltugas);
            ?>
            

            I've try to solve the question by putting asort($masatugas); and asort($biltugas); but the output came out like this
            Array ( [1] => 0.3 [0] => 0.6 )
            (this is $masatugas)
            Array ( [0] => 1 [1] => 2 )
            (this is $biltugas)
            The $masatugas can be sort from smallest to biggest and the array [1] and [0] also already correct but the $biltugas can't make it accordingly to $masatugas because it suppose should be
            [1] =>2
            [0] =>1
            Can anyone please help?

              Well, they're two completely different arrays - no reason to expect anything that happens to one to happen to the other.

              If the items truly are paired up like that, then it would make sense to make a structure that really does pair them up.

              But you won't be able to do that until you can be sure that the two arrays returned will have the same number of elements; otherwise sorting one array according to how the other array is sorted will be meaningless.

              But let's assume you've done this. You've got $biltugas and $masatugas arrays. There are $n elements in both.
              $both_arrays = array();
              for($i=0; $i<$n; $i++)
              {
              $both_arrays[$i] = array('biltugas'=>$biltugas[$i], 'masatugas'=>$masatugas[$i]);
              }
              [/code]
              Now you've got an array that looks like

              array(
                  array('biltugas'=>1, 'masutagas'=>0.6),
                  array('biltugas'=>2, 'masutagas'=>0.3),
              );

              Using a comparison function that returns -1, 0, 1 depending on whether $a['masatugas'] is less than, equal to, or greater than $b['masatugas'], and usort() to call it that becomes

              array(
                  array('biltugas'=>2, 'masutagas'=>0.3),
                  array('biltugas'=>1, 'masutagas'=>0.6),
              );
                Write a Reply...