ankme Posted May 19, 2011 Share Posted May 19, 2011 I have a file that contains, among others, some links to other files. Those files contain links to other files and so on. What I want to do is to check if a file has already been opened, and if this is true I want the file not to be opened again. For this I created an array, but when the code returns to the first file the array is reseted. Can someone please help me? This is the code I created (I`m a beginer in php). Thanks function readImportFiles($taxonomyFile, $target_path, $fileName='', $ArrayOfTaxonomies = array()) { while(!feof($taxonomyFile)) { $line = fgets($taxonomyFile); //code for getting the $schemaLocation if ($schemaLocation && !in_array($schemaLocation, $ArrayOfTaxonomies)) { $ArrayOfTaxonomies[]= $schemaLocation; $importSchema = fopen($schemaLocation, "r"); $target_path = substr($schemaLocation, 0, (strlen($schemaLocation) - strlen(strrchr($schemaLocation, "/")))); readImportFiles($importSchema, $target_path, $schemaLocation, $ArrayOfTaxonomies); } } } Quote Link to comment https://forums.phpfreaks.com/topic/236871-keep-elements-in-array-in-while/ Share on other sites More sharing options...
DavidAM Posted May 19, 2011 Share Posted May 19, 2011 Pass the array as a reference so that any changes you make are made to the original copy. function readImportFiles($taxonomyFile, $target_path, $fileName='', &$ArrayOfTaxonomies = array()) // << Pass array as reference { while(!feof($taxonomyFile)) { $line = fgets($taxonomyFile); //code for getting the $schemaLocation if ($schemaLocation && !in_array($schemaLocation, $ArrayOfTaxonomies)) { $ArrayOfTaxonomies[]= $schemaLocation; $importSchema = fopen($schemaLocation, "r"); $target_path = substr($schemaLocation, 0, (strlen($schemaLocation) - strlen(strrchr($schemaLocation, "/")))); readImportFiles($importSchema, $target_path, $schemaLocation, $ArrayOfTaxonomies); } } } Quote Link to comment https://forums.phpfreaks.com/topic/236871-keep-elements-in-array-in-while/#findComment-1217722 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.