RE: array insights

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

 



On 03 May 2006 18:27, Jason Gerfen wrote:

> I am looking for some information on how to do this the correct way,
> here is the data I am working with:
> 
> Array
> (
>     [hostname-0] => hostname
>     [mac-0] => 00:0a:b3:aa:00:5d
>     [ip-0] => 192.168.0.1
>     [subnet] => MMC-Subnet
>     [group] => MMC-PXE
>     [hostname-1] => tester01
>     [mac-1] => 00:09:02:bb:cc:zz
>     [ip-1] => 192.168.0.2
>     [hostname-2] => new-test
>     [mac-2] => 00:09:02:bb:cc:99
>     [ip-2] => 192.168.0.3
>     [cmd] => edit
>     [import] => Re-submit
> )
> 
> Here is how I need the above data to look when finished processing.
> 
> Array
> (
>     [0] => Array
>         (
>             [0] => hostname
>             [1] => 00:0a:b3:aa:00:5d
>             [2] => 192.168.0.1
>         )
> 
>     [1] => Array
>         (
>             [0] => tester01
>             [1] => 00:09:02:bb:cc:zz
>             [2] => 192.168.0.2
>         )
> 
>     [2] => Array
>         (
>             [0] => new-test
>             [1] => 00:09:02:bb:cc:99
>             [2] => 192.168.0.3
>         )
> 
> )

Here's one way:

   $new = array();
   for ($i=0; isset($array["hostname-$i"]; ++$i):
      $new[$i] = array($array["hostname-$i"], $array["mac-$i"], $array["ip-$i"]);
   endfor;

If your list of prefixes is likely to change at all (e.g. to add a port number), you could generalize it like this:

   $prefixes = array('hostname', 'mac', 'ip', 'port');
   $new = array();
   for ($i=0; isset($array["{$prefixes[0]}-$i"]; ++$i):
      foreach ($prefixes as $k=>$pfx):
         $new[$i][$k] = $array["{$pfx}-$i"];
      endforeach;
   endfor;

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 


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

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