RE: Muti-Dimensional Array Help Please

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

 



> -----Original Message-----
> From: Nick Wilson [mailto:nick@xxxxxxxxxxxxxx]
> Sent: Thursday, October 07, 2004 6:40 AM
> To: php-general
> Subject:  Muti-Dimensional Array Help Please
> 
> hello all,
> 
> I have some text files in a directory that contain newline delimited IP
> addresses. Here is what i would like to do with them, and what i have
> achieved so far:
> 
> I read the files into a multi-dimensional array like: (pseudo code)
> 
> array(IP_blocks) = (
> File_number[1] = array('ip1', 'ip2', 'ip3')
> File_number[2] = array('ip1', 'ip2', 'ip3')
> File_number[3] = array('ip1', 'ip2', 'ip3')
> )
> 
> the ip files have variable numbers of ips, some have 5 ips, some 40
> etc..
> 
> What i want to do is to run a loop through the whole thing doing a task
> on each IP address. Easy so far right? - Well, where im stuck is that i
> want to operate on them like this:
> 
> do_stuff(File_number[1][ip1])
> do_stuff(File_number[2][ip1])
> do_stuff(File_number[3][ip1])
> do_stuff(File_number[1][ip2])
> do_stuff(File_number[2][ip2])
> do_stuff(File_number[3][ip2])
> 
> I cant even get that far LoL! but then there is the problem of what
> happens when there is no IP left in an array? (its a short file of
> ips..)? - I need to start that particular array back at the beggining
> but continue the cycle (it doesnt matter that an ip is worked on more
> than once..)
> 
> Here is the *actual* code I have so far:
> 
> ##CODE
> <?php
>   /*
>   * Open the proxies dir and read all of the
>   * ips contained within each file into a mutidimensional
>   * array ($machines)
>   */
>   $dir = dir('proxies');
>   while(FALSE !== ($file = $dir->read())) {
>     if($file != '.' && $file != '..') {
>       $machines[$file] = file($dir->path . '/' .$file);
>     }
>   }
>   $dir->close();
> 
>   foreach($machines as $machineId => $ips) {
>     // do stuff here
>   }
> ###END CODE
> 
> Im not really expecting anyone to be able to do my work for me but some
> *guidance* regardig how to tackle this problem would be magnificent...

It looks like you're hading in the right direction.  You've grabbed an array
of each file.  Then cycle through that array and read data from each file.
The fread function will allow you to pull data from each file line by line,
so you can store each line in an array.  Using your idea of pseudo-code:

$data = array();
$i = 1;
foreach ( file ) {
	$n = 1;
	fp = fopen
	while ($line = fread() ){
		$data[$i][$n] = $line;
	}
}

foreach ($data as $key => $line ) {
	foreach ($line as $line_number => $ip) {
		// do stuff
	}
}

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