willg Posted September 27, 2007 Share Posted September 27, 2007 Hi, I have an array with the following structure 1 => Title: etc etc, Authors: etc etc, Filename: etc etc 2 => Title: etc etc, Authors: etc etc, Filename: etc etc 3 => Title: etc etc, Authors: etc etc, Filename: etc etc 4 => Title: etc etc, Authors: etc etc, Filename: etc etc 5 => Title: etc etc, Authors: etc etc, Filename: etc etc I have been trying (without success) to save each value as a text file with the title and authors as the content and the filename as the filename. I can easily isolate the needed texts with regex (so don't waste your time on that part) but am struggling with the "foreach" PHP part. Please help (!) Thanks for reading, Will Quote Link to comment https://forums.phpfreaks.com/topic/70954-save-array-values/ Share on other sites More sharing options...
BlueSkyIS Posted September 27, 2007 Share Posted September 27, 2007 foreach ($somearray AS $someinfo) { echo $someinfo; // This will be the entire string, Title: etc etc, Authors: etc etc, Filename: etc etc } Quote Link to comment https://forums.phpfreaks.com/topic/70954-save-array-values/#findComment-356710 Share on other sites More sharing options...
willg Posted September 27, 2007 Author Share Posted September 27, 2007 Thanks for the quick reply. How do the "fopen", "fwrite" and "fclose" statments fit in to the "foreach" statement? e.g. do they go inside or outside? (however I try it doesn't work) Quote Link to comment https://forums.phpfreaks.com/topic/70954-save-array-values/#findComment-356713 Share on other sites More sharing options...
marcus Posted September 27, 2007 Share Posted September 27, 2007 If you're opening one file, outside, fwrite > inside. Quote Link to comment https://forums.phpfreaks.com/topic/70954-save-array-values/#findComment-356715 Share on other sites More sharing options...
BlueSkyIS Posted September 27, 2007 Share Posted September 27, 2007 Not one file. one file for each title. here is how i might do it: foreach ($somearray AS $someinfo) { $title_str = howeveryouregettingthetitle(); $author_str = howeveryouregettingtheauthor(); $filename = howeveryouregettingthefilename(); $fh = fopen("/path/to/$filename", "w") or die("fopen failed"); fwrite($fh, "$title_str $author_str"); fclose($fh); } Quote Link to comment https://forums.phpfreaks.com/topic/70954-save-array-values/#findComment-356716 Share on other sites More sharing options...
willg Posted September 27, 2007 Author Share Posted September 27, 2007 Hi, The code that I tried (putting in your code as well) is below. I'm still going wrong somewhere unfortunately. The below code produced one text file (instead of many) with the correct content for that particular value but no filename (it was just ".txt"). Any ideas? foreach ($replace10 as $key => $val) { $matchtitle = preg_match("@TITLE\:\s(.*?)\r\n@", $val, $outtitle); $title = preg_replace("@TITLE\:\s@", "", $outtitle[0]); $matchauthors = preg_match("@AUTHORS\:\s(.*?)\r\n@", $val, $outauthors); $authors = preg_replace("@AUTHORS\:\s@", "", $outauthors[0]); $matchcitation = preg_match("@CITATION\:\s(.*?)\r\n@", $val, $outcitation); $citation = preg_replace("@CITATION\:\s@", "", $outcitation[0]); $matchfile = preg_match_all("@FILE\:\s(.*?)\r\n@", $val, $outFILE); $file = preg_replace("@FILE\:\s@", "", $outFILE[0]); $desccontent = '<title>'.$title.' ('.$citation.') - '.$publisher.'</title> <meta name="description" content="'.$authors.'">'; $fh = fopen("$file.txt", "w") or die("fopen failed"); fwrite($fh, "$desccontent"); fclose($fh); } Quote Link to comment https://forums.phpfreaks.com/topic/70954-save-array-values/#findComment-356735 Share on other sites More sharing options...
willg Posted September 28, 2007 Author Share Posted September 28, 2007 Hello PHP ppl! Well, I got a solution: foreach ($combined as $key => $val) { file_put_contents($key, $val); } Note that you have to configure your array to have the filename as the key and the file contents as the value. Quote Link to comment https://forums.phpfreaks.com/topic/70954-save-array-values/#findComment-357086 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.