reading file into a keyword-indexed array

From: Date: Wed, 05 Feb 2003 00:03:18 +0000
Subject: reading file into a keyword-indexed array
Groups: php.general 
Request: Send a blank email to php-general+get-134345@lists.php.net to get a copy of this message
I would like to read and write user-settings to a keyword-value file and be able to extract it. The following code works fine but i wondered if there was a more elegant/direct way of doing this. -Durwood $file = "user_settings.txt"; // Read user settings from file if it exists, otherwise set to defaults and // save them ... a separate method exists for users to change their settings. if ($is_readable($file)) { $parm = read_defaults($file); } else { $parm['method'] = "bsort"; $parm['tsize'] = 200; $parm['per_row'] = 5; $parm['sortby'] = "last_name"; $parm['order'] = "ascending"; write_defaults($file, $parm); } // Read keyword-value pairs from file into array function read_defaults($default_file) { $fp = file($default_file); foreach ($fp as $line) { $line = trim($line); list ($key, $value) = explode(" ", $line); $param[$key] = $value; } return $param; } // Write keyword-value pairs from array function write_defaults($default_file, $param) { $fp = fopen($default_file, "w"); while (list($key, $value) = each ($param)) fwrite ($fp, "$key $value\n"); fclose($fp); }

« previous php.general (#134345) next »