Jump to content

Keep elements in array in while


ankme

Recommended Posts

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);

 

}

}

}

Link to comment
https://forums.phpfreaks.com/topic/236871-keep-elements-in-array-in-while/
Share on other sites

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);
    }
  }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.