Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 23rd, 2007, 10:44 AM
Authorized User
Join Date: Jul 2007
Location: Winston-Salem, nc, USA.
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>";

Reply With Quote
  #2 (permalink)  
Old September 5th, 2007, 03:24 PM
Authorized User
Join Date: Sep 2007
Location: , , .
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.


Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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

All times are GMT -4. The time now is 12:45 AM.

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