Wrox Programmer Forums
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old August 23rd, 2007, 10:44 AM
Authorized User
Join Date: Jul 2007
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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!

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

Old September 5th, 2007, 03:24 PM
Authorized User
Join Date: Sep 2007
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts

Is there a reason why you're not simply using this?
sort( $arrayname );
This works in ascending order. rsort() for reverse.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Session variable weirdness LenexaKS ASP.NET 2.0 Professional 5 June 4th, 2007 11:05 AM
GridView HeaderStyle Weirdness Bob Bedell ASP.NET 2.0 Basics 2 December 25th, 2006 02:28 PM
DateDiff Weirdness SerranoG Access VBA 1 July 16th, 2004 12:38 AM
Connection String Weirdness shock VB Databases Basics 0 September 26th, 2003 10:01 PM

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