Re: Muti-Dimensional Array Help Please

From: Date: Thu, 07 Oct 2004 13:35:08 +0000
Subject: Re: Muti-Dimensional Array Help Please
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-198825@lists.php.net to get a copy of this message
Nick Wilson wrote:
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.. if it doesn't matter in what order they are all processed, then you could so it like this:
<?php foreach($machines as $machineId => $ips) { foreach($ips as $ip) {
      do_stuff($ip);
} } ?> However, if it does matter, then I suggest a different way. This would order them by their 2nd index, not the first (meaning by $machines[1][1], $machines[2][1], etc); <?php $max = 0; foreach($machines as $machineId => $ips) { $max = ($max >= count($ips) ? $max : count($ips)); } for($i=0;$i<$max;$i++) { foreach($machines as $machineId => $ips) {
      if(isset($ips[$i])) {
         do_stuff($ips[$i]);
      } else {
         continue;
      }
} } ?> but that's just my point of view ;)

« previous php.general (#198825) next »