在数组中添加一个数组到一个数组中的多个根

问题描述:

我有一个JSON解码数组,如下所示,它具有到根的路径。在数组中添加一个数组到一个数组中的多个根

$file_path = json_decode($_REQUEST['file_path']); 

正如我们所知,我们可以添加多个根,如下所示。

$opts = array(
'roots' => array(
    array(
     'driver'  => 'LocalFileSystem',   // driver for accessing file system (REQUIRED) 
     'path'   => 'path/to/files/first_root', // path to files (REQUIRED) 
     'URL'   => 'http://localhost/files/first_root/', // URL to files (REQUIRED) 
     'alias'   => 'First home', // The name to replace your actual path name. (OPTIONAL) 
     'accessControl' => 'access'  // disable and hide dot starting files (OPTIONAL) 
    ), 
    array(
     'driver'  => 'LocalFileSystem', 
     'path'   => 'path/to/files/second_root', 
     'URL'   => 'http://localhost/files/second_root/', 
     'alias'   => 'Second home' 
    ) 
) 

);

我想动态地添加我的数组路径,如下所示。

foreach ($file_path as $path){ 
$array_1[] = array(
    'driver'  => 'LocalFileSystem',   // driver for accessing file system (REQUIRED) 
    'path'   => $path,     // path to files (REQUIRED) 
    'URL'   => $path, // URL to files (REQUIRED) 
    'trashHash'  => 't1_Lw',      // elFinder's hash of trash folder 
    'winHashFix' => DIRECTORY_SEPARATOR !== '/', // to make hash same to Linux one on windows too 
    'uploadDeny' => array('all', '*'),    // All Mimetypes not allowed to upload 
    'uploadOrder' => array('deny', 'allow'),  // allowed Mimetype `image` and `text/plain` only 
    'accessControl' => 'access'      // disable and hide dot starting files (OPTIONAL) 
); 

}

但问题是,这将增加额外的键对每个对象的前面。如下。

array(0 => 
array(
     'driver'  => 'LocalFileSystem',  
     'path'   => 'path/to/files/first_root', 
     'URL'   => 'http://localhost/files/first_root/', 
     'alias'   => 'First home', 
     'accessControl' => 'access' 
    ), 
1 => 
    array(
     'driver'  => 'LocalFileSystem', 
     'path'   => 'path/to/files/second_root', 
     'URL'   => 'http://localhost/files/second_root/', 
     'alias'   => 'Second home' 
    )); 

如何删除额外的数组键或在elFinder中完成此任务的方式。

+0

你的意思是删除0和1,这是数组索引,无法删除它。 –

+0

我们不能把它看作简单的数组,像数组(“test”,“tes”,“te”)吗? –

+0

您可以像这样分配值,但是当您需要访问这些项目时,您需要键/索引 –

最后我找到了解决方案。希望这会有所帮助,如果有人得到这个问题。

循环$ opts数组并将数据推送到“根”键并按如下方式启动。

$opts['roots'] = array(); 
foreach ($file_path as $path) { 
    array_push($opts['roots'],array (
      'driver'  => 'LocalFileSystem',   // driver for accessing file system (REQUIRED) 
      'path'   => "$path/",     // path to files (REQUIRED) 
      'URL'   => "$path", // URL to files (REQUIRED) 
      'trashHash'  => 't1_Lw',      // elFinder's hash of trash folder 
      'winHashFix' => DIRECTORY_SEPARATOR !== '/', // to make hash same to Linux one on windows too 
      'uploadDeny' => array('all', '*'),    // All Mimetypes not allowed to upload 
      'uploadOrder' => array('deny', 'allow'),  // allowed Mimetype `image` and `text/plain` only 
      'accessControl' => 'access'      // disable and hide dot starting files (OPTIONAL) 
     ) 
    ); 
} 

$connector = new elFinderConnector(new elFinder($opts)); 
$connector->run();