-1

I have this php code

$files_array = array();
$files_array = glob('*.aria2');
$files_array = str_replace('.aria2', '', $files_array);
array_push($files_array,"processing");

If i print result of $files_array

Array
(
    [0] => [LiveFish] Vanguard [AAC]
    [1] => [Riycou] K-Project [MP4 AAC 720p]
    [2] => processing
)

I use in_array code

if(!in_array($ff, $files_array))
{
listFolderFiles($dir.'/'.$ff);

}

Php return me an error

PHP Warning:  in_array() expects parameter 2 to be array, null given 

My $files_array variable seems okay, if I use print_r, it was able to display the array value, why would php give me an error mention parameter 2 was null

Below is my full code structure

$files_array = array();
$files_array = glob('*.aria2');
$files_array = str_replace('.aria2', '', $files_array);
array_push($files_array,"processing");
print_r($files_array);


function listFolderFiles($dir){
    $ffs = scandir($dir);
    $i = 0;
    $list = array();

    foreach ( $ffs as $ff ){
        if ( $ff != '.' && $ff != '..' ){
            if ( strlen($ff)>=5 ) {
                $extension = array('.mkv', '.avi', '.mp4');
                $file_extension = substr($ff, -4);
                if ( in_array($file_extension,$extension )) {
                    $list[] = $ff;
                    echo dirname($ff) . $ff . "<br/>";
                    $fileName = $dir.'/'.$ff;
                }
            }
            if( is_dir($dir.'/'.$ff) )
                if(!in_array($ff, $files_array)) {
                    listFolderFiles($dir.'/'.$ff);
                }
           }
        }
    return $list;
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
user3412075
  • 129
  • 1
  • 9
  • If you move `$files_array = array();` just above the if block, does it work? – Anthony Aug 26 '14 at 15:37
  • 1
    You are unsetting your array after the first loop, but continuing to loop, so the second time around the array is unset and you don't get anything useful. I think that's correct anyway, your indenting is throwing me off. – scragar Aug 26 '14 at 15:37
  • I took away unset, and rearrange my code, please check the edited version . Issue still persist. – user3412075 Aug 26 '14 at 15:39

3 Answers3

1
unset($files_array);

Don't do that inside the loop, or terminate the loop right after doing this with break or return. But don't continue looping back into the in_array call, of course that will break.

Since your edit you moved setting the array to outside the function, as such it is no longer in scope. Either import it using global keyword, or pass it as a parameter.

Read more about variable scope and global on PHP.net.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
1

Put this line in the beginning of your function:

global $files_array;

Documentation: https://php.net/manual/en/language.variables.scope.php - second example.

Or pass as a parameter to function:

function listFolderFiles($dir, $files_array)
Stanislav Shabalin
  • 19,028
  • 3
  • 18
  • 18
1

Since you are using the $files_array inside a function, it must be in global scope. Use global keyword before the variable:

global $files_array;

At the start of the function.

function listFolderFiles($dir){
  global $files_array;
  $ffs = scandir($dir);
  $i = 0;
  $list = array();

  foreach ( $ffs as $ff ){
    if ( $ff != '.' && $ff != '..' ){
      if ( strlen($ff)>=5 ) {

        $extension = array('.mkv', '.avi', '.mp4');
        $file_extension = substr($ff, -4);
        if ( in_array($file_extension,$extension ))
        {
          $list[] = $ff;
          echo dirname($ff) . $ff . "<br/>";
          $fileName = $dir.'/'.$ff;

        }
      }
      if( is_dir($dir.'/'.$ff) )
        if(!in_array($ff, $files_array))
        {
          listFolderFiles($dir.'/'.$ff);
        }
    }
  }
  return $list;
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252