[PHP] 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... Thankyou.. -- Nick W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Muti-Dimensional Array Help Please
-Original Message- From: Nick Wilson [mailto:[EMAIL PROTECTED] Sent: Thursday, October 07, 2004 6:40 AM To: php-general Subject: [PHP] 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
Re: [PHP] Muti-Dimensional Array Help Please
* and then Ed Lazor declared 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 } } hehe, not sure i get it but im playing with it ;-) i need to see code in action for my head to understand it. I did have one rather good though though (i think): If i can create an array with all of the ips in order like this 123.123.123.1 // 1st IP from machine 1 123.123.123.2 // 1st IP from machine 2 123.123.123.3 // 1st IP from machine 3 123.123.123.4 // 2nd IP from machine 1 123.123.123.5 // 2nd IP from machine 2 123.123.123.4 // 2nd IP from machine 3 etc... then i would have *exactly* what the application needs. Does that make the problem any easier? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Nick W -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php