// Save an array as tab seperated text file // function Write_tabbed_file ($filepath, $array, $save _keys=false) { $content = "; Reset ($array); while (list ($key, $val) = each ($array)) { Replace tabs in keys and values to [space] $key = Str_replace ("\ T", "", $key); $val = Str_replace ("\ T", "", $val); if ($save _keys) {$content. = $key. " \ t "; } Create line: $content. = (Is_array ($val))? Implode ("\ T", $val): $val; $content. = "\ n"; } if (file_exists ($filepath) &&!is_writeable ($filepath)) { return false; } if ($fp = fopen ($filepath, ' w+ ')) { Fwrite ($fp, $content); Fclose ($FP); } else {return false;} return true; } // Load a tab seperated text file as array // function Load_tabbed_file ($filepath, $load _keys=false) { $array = Array (); if (!file_exists ($filepath)) {return $array;} $content = file ($filepath); for ($x =0; $x < count ($content); $x + +) { if (Trim ($content [$x])! = ") { $line = explode ("\ t", Trim ($content [$x])); if ($load _keys) { $key = Array_shift ($line); $array [$key] = $line; } else {$array [] = $line;} } } return $array; } /* * * Example Usage: */ $array = Array ( ' line1 ' = = Array (' data-1-1 ', ' data-1-2 ', ' data-1-3 '), ' line2 ' = = Array (' data-2-1 ', ' data-2-2 ', ' data-2-3 '), ' Line3 ' = = Array (' data-3-1 ', ' data-3-2 ', ' data-3-3 '), ' Line4 ' = ' foobar ', ' Line5 ' = ' Hello World ' ); Save the array to the Data.txt file: Write_tabbed_file (' Data.txt ', $array, true); /* The data.txt content looks like this: Line1 data-1-1 data-1-2 Data-1-3 Line2 data-2-1 data-2-2 data-2-3 Line3 data-3-1 data-3-2 data-3-3 Line4 Foobar Line5 Hello World */ Load the saved array: $reloaded _array = load_tabbed_file (' Data.txt ', true); Print_r ($reloaded _array); Returns the array from above |