Jump to content

Why does directory listing wih array_push gives warning "the first argument is not an array" ?

Go to solution Solved by requinix,

Recommended Posts

I read that array_push() can give warning when the first argument is not an array from php manual. However, my below code is for directory listing. Although there is no int variable in directory names(I checked all filenames-all string- but maybe implicit conversion is done??) randomly it gives warning. And it when I wrote the array  with var_dump() it writes some strange "int(1)", "int(4)" etc. as a result. Can someone explain where is my mistake?

My code for recursive directory listing with array_push() function ;

  function listAllFiles($dir) {
    $array = array_diff(scandir($dir), array('.', '..')); // remove "." and ".." folders
    foreach ($array as $f) {
      $sf = $dir.DIRECTORY_SEPARATOR.$f ;
      if(is_dir($sf)) {
          	echo "...no array...";
        } // end is_array check
        $array = array_push($array, listAllFiles($sf));
        echo "<br><b>".$sf." is a directory</b><br>";
      } else if(is_file($sf)) { echo "<br>".$sf." is a file"; } // end "if file/directory" check
    }  // end outer for loop
    return $array;


Link to comment
Share on other sites

3 minutes ago, requinix said:

What is the second argument to array_push supposed to be? What is the return value?

Thanks requinix. In manual page it says that : 

"array_push() treats array as a stack, and pushes the passed variables onto the end of array. The length of array increases by the number of variables pushed."

It should be "mixed" variable. In my code it should be an array(named $array) since it calls recursively the same function... So why the first argument gives an error then? Is it better to use array_merge() instead of array_push() then? I will try...

Link to comment
Share on other sites

  • Solution

The second argument to array_push is the item you want to push. It is not an array of items you want to append. So that's a problem, though a bit separate from what you're looking at now.

How about the return value?

  • Thanks 1
Link to comment
Share on other sites

2 minutes ago, requinix said:

How about the return value?

:thumb-up: Yes, you are right, it is integer ! Oh my god! My mistake is using array_push()... Thanks requinix! So, my final code using array_merge() was completely warning free now. 

 $array = array_merge($array, listAllFiles($sf));


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • 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.