mkosmosports Posted April 6, 2007 Share Posted April 6, 2007 Hey, Ive got the below array: [geninfo] => Array ( [groups] => Array ( [13] => African Nations Cup XXV [7] => Champions League [1] => English Premiership ) ) And Id like to write its contents to a file, but that content needs to be written in a certain format. Id like that format to be <option value="(array key)">(array value)</option>... Ive tried some variations using the file_put_contents and implode functions but I havent been able to arrive to what I want to do... Can someone help out? Thanks for any ideas! mkosmosports Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/ Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 foreach ($array as $key => $val) { if (is_array($val)) { foreach ($val as $key2 => $val2) { $write .= '<option="'. $key2 . '">'.$val2.'</option>'; } } } $fp = fopen('writeto.txt', '+w'); fwrite($fp, $write); fclose($fp); Given the example array that should work. Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223154 Share on other sites More sharing options...
mkosmosports Posted April 6, 2007 Author Share Posted April 6, 2007 Hey, Thanks for your help on this one frost... Im getting an error related to the write variable... Notice: Undefined variable: write in C:\Web\Apache2\htdocs\index.html on line 122 Warning: fopen(writeto.txt) [function.fopen]: failed to open stream: No error in C:\Web\Apache2\htdocs\index.html on line 127 Warning: fwrite(): supplied argument is not a valid stream resource in C:\Web\Apache2\htdocs\index.html on line 128 Warning: fclose(): supplied argument is not a valid stream resource in C:\Web\Apache2\htdocs\index.html on line 129 Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223173 Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 $write = ""; // declare variable to avoid notice which does not really mean anything foreach ($array as $key => $val) { if (is_array($val)) { foreach ($val as $key2 => $val2) { $write .= '<option="'. $key2 . '">'.$val2.'</option>'; } } } $fp = fopen('writeto.txt', '+w') or DIE("Could not open file to write to"); fwrite($fp, $write); fclose($fp); http://us2.php.net/fopen Make sure you have write permissions for that directory. IE CHMOD 777 Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223176 Share on other sites More sharing options...
mkosmosports Posted April 6, 2007 Author Share Posted April 6, 2007 Works beautifully! I appreciate the help frost. Can you explain to me what the .= means when you define the $write variable? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223179 Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 Basically it concatenates or "appends" the data after it to the variable. IE: $cat = "How did the "; $cat .= "cat get so fat?"; print $cat; // will print "How did the cat get so fat?" // the same as $cat = "How did the " . "cat get so fat?"; It can also be used like += for numbers IE: $num = 5; $num += 5; print $num; // should print 10. Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223183 Share on other sites More sharing options...
mkosmosports Posted April 6, 2007 Author Share Posted April 6, 2007 Thats a pretty nice shortcut. Thanks frost. Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223188 Share on other sites More sharing options...
mkosmosports Posted April 6, 2007 Author Share Posted April 6, 2007 Damn, looks like Ive got one last question for you. Im trying to insert a line break (\n) after every line thats inserted into this new file but its always printing it out as opposed to applying the line break.. $write .= '<option="'. $key . '">'.$val.'</option>'"\n"'; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223191 Share on other sites More sharing options...
per1os Posted April 6, 2007 Share Posted April 6, 2007 $write .= '<option="'. $key . '">'.$val."</option>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223206 Share on other sites More sharing options...
mkosmosports Posted April 6, 2007 Author Share Posted April 6, 2007 Man, I just cant seem to finish this off. Using the code below Im attempting to run the query results of 4 different queries into four separate files.... $allgroups = @mysql_query("SELECT id, name from sf_group ORDER BY name"); $allteams = @mysql_query("SELECT id, quick_name from sf_team WHERE linkable = 'y' ORDER BY quick_name"); $allplayers = @mysql_query("SELECT id, nick_name from sf_player ORDER BY nick_name"); $allarticles = @mysql_query("SELECT id, title from sf_articles"); $type_arr = array("allgroups" => array("groups","name"),"allteams" => array("teams","quick_name"),"allplayers" => array("players","nick_name"),"allarticles" => array("articles","title")); $write = ""; foreach($type_arr as $key => $value) { while($row = mysql_fetch_array($$key,MYSQL_ASSOC)) { $write .= '<option value="'. $row["id"] . '">'.$row["$value[1]"]."</option>\n"; } $file = fopen("$value[0]"."_"."select.html", 'w+') or DIE("Could not open file to write to"); fwrite($file, $write); fclose($file); } The problem Im having now is that the files that are being written contain more than theyre supposed to. I want them each to contain only the contents from the query currently being looped through but what happens is the first file is correct but the second one contains the query results of the first plus its results, and so on after that.... I want it to only write the contents of the query its currently looping through... Any ideas? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223220 Share on other sites More sharing options...
mkosmosports Posted April 7, 2007 Author Share Posted April 7, 2007 Nevermind frost. I got it figured out. The definition of the $write variable needed to happen within the foreach.... Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223357 Share on other sites More sharing options...
per1os Posted April 7, 2007 Share Posted April 7, 2007 Glad you got it figured out. I like it when people figure out their problems on their own. It means you actually learned something. Glad to be of service. Quote Link to comment https://forums.phpfreaks.com/topic/45939-write-array-contents-into-file/#findComment-223378 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.