RE: Help with Array Sorting Please

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 07 October 2004 17:29, Nick Wilson wrote:

> Hi,
> 
> If you scroll up the list you'll see another thread of mine, this is
> related. The reason for a new thread is that i've made a lot
> of progress
> but the old was getting really confusing..
> 
> Here's the scoop:
> 
> I have an array like this:
> 
> 123 => 1  // each value may have few or many duplicate values 321 => 1
> 543 => 2
> 432 => 2 // like here, more 2's than 1's see?
> 566 => 2
> 568 => 2
> 999 => 3
> 878 => 3
> 444 => 3

That's not terribly good, as you are using the "unknowns" (ip addresses) as keys to the "knowns" (machine IDs).  It would be better keying off the machine IDs, although this does lead to a 2-level array; something like:

   array( 1 => array(123),
          2 => array(543, 432, 566, 568),
          3 => array(999, 878, 444)
        )

> Now, the keys are not the issue. What i would really like, is for
> that array, to end up like this: 
> 
> 123 => 1
> 543 => 2
> 999 => 3
> 321 => 1
> 432 => 2
> 878 => 3
> 123 => 1
> 566 => 2

OK. So you don't really want an array out of this, but a sequence of machine ID => IP address pairs.  Do you want this to cycle ad infinitum, or just until you've hit every IP address at least once?  In either case, using my restructured array, I think your basic loop looks something like this:

   // don't assume machine IDs are sequential:
   $mach_ids = array_keys($array);

   // this loop may look odd, but can't foreach $array itself as
   // that gives a copy and following code needs original.
   foreach ($mach_ids as $m_id):
      reset($array[$m_id]);
   endforeach

   foreach ($mach_ids as $m_id):
      list(,$ip) = each($array[$m_id]);
      if ($ip===FALSE):
         reset($array[$m_id]);
         list(,$ip) = each($array[$m_id]);
      endif;

      // do stuff with $m_id and $ip

   endforeach;

That's an endless loop -- there's no code in there to terminate it, so if you want to stop at some point you'll have to figure out the conditions to test and break out.

Usual disclaimer -- it's off the top of my head and untested!

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 283 2600 extn 4730      Fax:  +44 113 283 3211 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux