p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Beginning PHP (http://p2p.wrox.com/forumdisplay.php?f=95)
-   -   Array weirdness? (http://p2p.wrox.com/showthread.php?t=61441)

oldmainframehack August 23rd, 2007 10:44 AM

Array weirdness?
 
Hi, folks!
I must be misunderstanding something here....

I'm trying to fill an array, and sort it with the largest values at the top. What's got me puzzled is the way the array seems to be behaving....

During the process of filling the array elements, I see the values getting incremented, and such, but.... echoing the array element the SHOULD contain a 2, after the "filling loop" has finished, shows zero! I see it bump from 1 to 2, and then, after the filling loop, it's got 0....

Any help in de-mystifying this would be GREATLY appreciated!

Thanks for any pointers!
        Joe

Here's the code:

{
     $scount = array();
     $scount[$i] = array_fill(0,100,0);
//Load array
// $records is the number of records from the db.

      for ($i = 1; $i <= $records; $i++)
       {
/*
 $scode is the "stop code" from each record, a number between 0 and 100
*/
      $tctr = $scode[$i];
//Increment the element that maps to this code
      $scount[$tctr] ++;
      //echo "<p> code=" . $tctr . "</p>";
      //echo "<p> count=" . $scount[$tctr] . "</p>";
// next lines are test... show the value in one of the elements
     if ($tctr == 41)
               echo "<p> count=" . $scount[$tctr] . "</p>";
// .. shows twice, a 1 and a 2.....
       }
//... and now the value is zero??

    echo "<p> count=" . $scount[41] . "</p>";

// Now sort these puppies!
   for($x = 1; $x <= 100; $x++)
 {
    for($y = 1; $y <= 100; $y++)
    {
    if($scount[$x] > $scount[$y])
      {
        $hold = $scount[$x];
        $scount[$x] = $scount[$y];
        $scount[$y] = $hold;
      }
    }
 }
    echo "<table>";
    for ($i = 1; $i <= 10; $i++)
      echo "<tr> <td class='a'>" . $scount[$i] . "</td> </tr>";
    echo "</table>";
   }

JP

bluHatter September 5th, 2007 03:24 PM

Is there a reason why you're not simply using this?
Code:

sort( $arrayname );
This works in ascending order. rsort() for reverse.

http://www.php.net/manual/en/function.sort.php



All times are GMT -4. The time now is 07:22 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.