(Note: Sorry about the title… I tried to fix it, but can’t find a way to.)
I have some code I’m trying to improve by using only 1 foreach and for loop… It’s nothing more than a block of PHP code that outputs a list of servers, but since each server can sometimes have multiple IP addresses, well, it can get hairy with how it’s processed and I’m just not sure if I’m doing it in a way that could be better.
Here’s the code:
print '<div id="servers">';
print '<h3>Non-Database Servers</h3>';
print '<ul>';
//Create the server array...
//The $master array has both db and non_db servers in it. Separate them and grab only the non_db servers for this section of the page.
foreach($master[non_db_servers] as $machine){
//Each server can sometimes have more than 1 IP address for specific reasons...
for($i=0;$i<=count($machine["field_ip_addr"]);$i++){
//Check for null value.
if($machine["field_ip_addr"][$i][value] !== null){
//Determine if the server is a PRODUCTION or DEVELOPMENT box...
if($machine["field_stage"][$i][value] == 'Production (PROD)' || $machine["field_stage"][$i][value] == 'All Three (DEVL, CLNT, and PROD)'){
$ips[] = '<li><a href="'.$machine["nid"].'" title="">'.$machine["field_dns"][$i][value].' ('.$machine["field_ip_addr"][$i][value].')</a></li>';
}else{
$ips[] = '<li><a href="'.$machine["nid"].'" title="">'.$machine["field_dns"][$i][value].' ('.$machine["field_ip_addr"][$i][value].')</a></li>';
}
}
}//end for loop.
//Clean up.
unset($machine);
}
//This is where I would like to avoid having to use another FOREACH or FOR loop!
print_r(array_unique($ips));
print '</ul>';
print '</div>';
I think I used appropriate comments but if you have any questions about any of this, feel free to ask. Basically, I’m just trying to use less looping while providing the output I need (which is nothing more than links to each designated server the arrays have in them). I have having to use the array_unique at the end and I also believe I should be able to output directly from within each IF statement within that FOR loop, but I’m open to suggestions and appreciate any guidance someone might provide me with.
Could you provide some insight to how $master[non_db_servers] is populated? As creating unique entries in that array may be better suited elsewhere.
As for the looping, stop worrying about it. You could use implode but in all realty it is just going to loop over your array anyway. So you can either write the loop yourself, or let php loop over it via a different function call.
As best as I can tell, you have an array of machines, and each machine has an array of IPs (among other things). If that’s correct, then two loops to iterate over each of the two arrays is completely appropriate.
Jeff, that’s exactly what this is… I just thought there might be a better way to do it all.
cpradio, $master[non_db_servers] (to make a long story, short) is populated after an object is converted into an array via get_object_vars() and that original object is derived from a Drupal 6.x installation that uses the CCK module. (I thought it was appropriate to post this in here because of the PHP-specific concepts involved).
When I look at the code, I keep thinking to myself that there should be a way to use only 1 loop structure, but then again, we’re talking about arrays of arrays here, so I guess it works. Thanks everyone.