iPixel Posted July 1, 2008 Share Posted July 1, 2008 Is it possible to append into an array on a seperate file and save that php file so the array doesnt rever back to its original info ? example: $arrayname = array("a","b","c"); --- This is on file arrays.php then have a script append additional info into the array either using include("arrays.php") or fopen im now sure. I basically want to know how i could append ( array_push ? ) additional info into that array "d","e","f" etc... and save arrays.php so the next time u view that file d,e,f etc will be there. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/112838-update-php-files-dynamically/ Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 You could use a database....=X Probably your best bet. Or include the array, add to it, serialize it, then write it to a file. @_@ Quote Link to comment https://forums.phpfreaks.com/topic/112838-update-php-files-dynamically/#findComment-579551 Share on other sites More sharing options...
iPixel Posted July 1, 2008 Author Share Posted July 1, 2008 Yea i wish i could use a database but im stuck using a pre-made array because im just adding onto an old web application that unfortunatelly wasnt designed to use DB's. But you're saying include the old array, append new info to it and overwrite the arrays.php file ? with a newly inserted array ? Quote Link to comment https://forums.phpfreaks.com/topic/112838-update-php-files-dynamically/#findComment-579554 Share on other sites More sharing options...
lemmin Posted July 1, 2008 Share Posted July 1, 2008 Yeah that seems like an inefficient way to store data, but you could use preg_replace to change the text in the php file: preg_replace("[(\$variable=array\(\".+?\"[,\".+?\"]*)\)]", "$1,\"e\",\"f\",\"g\")", file_get_contents("phpfile.php")); I'm pretty sure that regex needs some cleaning up, but I hope you get the idea. Quote Link to comment https://forums.phpfreaks.com/topic/112838-update-php-files-dynamically/#findComment-579559 Share on other sites More sharing options...
DarkWater Posted July 1, 2008 Share Posted July 1, 2008 Yea i wish i could use a database but im stuck using a pre-made array because im just adding onto an old web application that unfortunatelly wasnt designed to use DB's. But you're saying include the old array, append new info to it and overwrite the arrays.php file ? with a newly inserted array ? I'm saying to have a serialized array in like, array.php and return it in the included file, then unserialize it, do some work on it, then reserialize it and write it to the file. Quote Link to comment https://forums.phpfreaks.com/topic/112838-update-php-files-dynamically/#findComment-579563 Share on other sites More sharing options...
sasa Posted July 1, 2008 Share Posted July 1, 2008 try arrays.php <?php $somearray = array(); ?> test.php <?php include('arrays.php'); print_r($somearray); $somearray[]= rand(1,1000); $new = var_export($somearray,true); $text = file_get_contents('arrays.php'); $tex = preg_replace('/(?<=\$somearray = )[^;]*(?=;)/is', $new, $text); $file = fopen('arrays.php', 'w'); fputs($file, $tex); fclose($file); ?> Quote Link to comment https://forums.phpfreaks.com/topic/112838-update-php-files-dynamically/#findComment-579606 Share on other sites More sharing options...
iPixel Posted July 2, 2008 Author Share Posted July 2, 2008 Wow I'm completely unfamiliar with using preg_replace, I never knew which mambo jambo characters to input for what. I'll give this a shot sasa, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/112838-update-php-files-dynamically/#findComment-579739 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.