简体   繁体   English

PHP动态填充数组

[英]PHP dynamically populating an array

I have an array that lists folders in a directory. 我有一个列出目录中文件夹的数组。 Until now, I've been hardcoding the folder names, but rather than do that, I thought I could easily create a script to parse the directory and just assign each folder name to the array. 到现在为止,我一直在对文件夹名称进行硬编码,但我没有这样做,我认为我可以轻松创建一个脚本来解析目录并将每个文件夹名称分配给数组。 That way, I could easily add folders and not have to touch the script again... 这样,我可以轻松添加文件夹,而不必再次触摸脚本...

The subject array creates an options list pulldown menu listing each folder... 主题数组创建一个选项列表下拉菜单,列出每个文件夹...

Currently, the array is hardcoded like so... 当前,数组是像这样硬编码的...

"options" => array("folder one" => "folder1", "folder two" => "folder2")), “ options” => array(“ folder one” =>“ folder1”,“ folder 2” =>“ folder2”)),

But I'm trying to make it dynamic based on whatever folders it finds in the given directory. 但是我正在尝试根据在给定目录中找到的任何文件夹使它动态化。

Here's the script I'm using to parse the directory and return the foldernames to the array. 这是我用来解析目录并将文件夹名称返回到数组的脚本。 It works fine. 工作正常。

function getDirectory( $path = '.', $level = 0 )
{
// Directories to ignore when listing output.
$ignore = array( '.', '..' );

// Open the directory to the handle $dh
$dh = @opendir( $path );

// Loop through the directory
while( false !== ( $file = readdir( $dh ) ) )
    {
    // Check that this file is not to be ignored
    if( !in_array( $file, $ignore ) )
        {
        // Show directories only
        if(is_dir( "$path/$file" ) )
            {
            // Re-call this same function but on a new directory.
            // this is what makes function recursive.
            //echo $file." => ".$file. ", ";
            // need to return the folders in the form expected by the array. Probably could just add the items directly to the array?
            $mydir2=$mydir2.'"'.$file.'" => "'.$file. '", ';
            getDirectory( "$path/$file", ($level+1) );
        }
    }
}
return $mydir2;
// Close the directory handle
closedir( $dh );
}

And here's my first take at getting those folders into the array... 这是我将这些文件夹放入阵列的第一步...

$mydir = getDirectory('/images/');
"options" => array($mydir)),

But obviously, that doesn't work correctly since its not feeding the array properly I just get a string in my options list... I'm sure this is an easy conversion step I'm missing... 但是很明显,这不能正常工作,因为它不能正确地馈入数组。我只是在选项列表中得到一个字符串...我确定这是一个简单的转换步骤,我很想念...

Why not just look at php.net ? 为什么不只看php.net It has several examples on recursive dir listing. 它具有递归目录清单的几个示例。

Here is one example: 这是一个例子:

<?php 
public static function getTreeFolders($sRootPath = UPLOAD_PATH_PROJECT, $iDepth = 0) { 
      $iDepth++; 
      $aDirs = array(); 
      $oDir = dir($sRootPath); 
      while(($sDir = $oDir->read()) !== false) { 
        if($sDir != '.' && $sDir != '..' && is_dir($sRootPath.$sDir)) { 
          $aDirs[$iDepth]['sName'][] = $sDir; 
          $aDirs[$iDepth]['aSub'][]  = self::getTreeFolders($sRootPath.$sDir.'/',$iDepth); 
        } 
      } 
      $oDir->close(); 
      return empty($aDirs) ? false : $aDirs; 
} 
?>

You want to create an array, not a string. 您要创建一个数组,而不是一个字符串。

// Replace
$mydir2=$mydir2.'"'.$file.'" => "'.$file. '", ';

// With
$mydir2[$file] = $file;

Also, close $dh before returning. 另外,在返回之前关闭$dh Now, closedir is never called. 现在,从不叫closedir。

Here is a simple function that will return an array of available directories, but it is not recursive in that it has a limited depth. 这是一个简单的函数,它将返回可用目录的数组,但是它的深度有限,因此不是递归的。 I like it because it is so simple: 我喜欢它,因为它很简单:

<?php
  function get_dirs( $path = '.' ){
    return glob( 
      '{' . 
        $path . '/*,'    . # Current Dir
        $path . '/*/*,'  . # One Level Down
        $path . '/*/*/*' . # Two Levels Down, etc.
      '}', GLOB_BRACE + GLOB_ONLYDIR );
  }
?>

You can use it like this: 您可以像这样使用它:

$dirs = get_dirs( WP_CONTENT_DIR . 'themes/clickbump_wp2/images' );

If you're using PHP5+ you might like scandir() , which is a built-in function that seems to do pretty much what you're after. 如果您使用的是PHP5 +,则可能会喜欢scandir() ,它是一个内置函数,似乎可以完成您所需要的工作。 Note that it lists all the entries in a folder - files, folders, . 请注意,它列出了文件夹中的所有条目-文件,文件夹, . and .. included. ..包括在内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM