Tuesday, March 20, 2012

Create an associative array in PHP

Some of you may dislike the distraction of a full set of working code with core lesson of associative arrays buried in it. Their are plenty of minimalist examples on the web, and when I wanted an example they were too minimalist. Core code for reference is in this color scheme.

deviceNickNames.txt
10.AF2551010800|no nickname
22.20F21A000000|no nickname
22.650E1B000000|no nickname
29.A29F09000000|no nickname
$deviceNickNamesTxt = "deviceNickNames.txt";
 if (file_exists($deviceNickNamesTxt)) {  
     $lines=explode("\n", file_get_contents($deviceNickNamesTxt)); 
       foreach($lines as $line) { 
                                 $items=explode("|",$line);
                          if(isset($items[1])) { $devicesB[$items[0]]= $items[1]; } 
                    }
                                     }

print "\n<form name=\"setupForm\" action=\"\" method=\"GET\">\n<table>\n<tr><th>Device Hardcoded Name</th><th>Human Friendly Name</th></tr>\n";
foreach($devicesB as $name => $value) {
print "\n<tr>";
print "\n<td valign=\"center\" align=\"center\">".$name."</td>";
print "\n<td valign=\"center\" align=\"center\">\n";
inputText($name."_NAME", $value, "");
submit("");
print "</td>\n</tr>\n";
    }
print "\n</table>\n</form>\n"
Device Hardcoded NameHuman Friendly Name
10.AF2551010800
22.20F21A000000
22.650E1B000000
29.A29F09000000

No comments: