0

当我 scandir() 它像这样在数组中返回

Array(
  [0]=>
  string(8) "DevTools"
  [1]=>
  string(17) "LB-Particles.phar"
  [2]=>
  string(11) "NoGrifersDP"
  [3]=>
  string(12) "PacketLogger"
  [4]=>
  string(24) "PocketMine-DevTools.phar"
)

所以我希望它得到这样的文件的完整路径

Array(
  [0]=>
  string(8) "C:\Users\USERNAME\PocketMine-MP\plugins\DevTools"
  [1]=>
  string(17) "C:\Users\USERNAME\PocketMine-MP\plugins\LB-Particles.phar"
  [2]=>
  string(11) "C:\Users\USERNAME\PocketMine-MP\plugins\NoGrifersDP"
  [3]=>
  string(12) "C:\Users\USERNAME\PocketMine-MP\plugins\PacketLogger"
  [4]=>
  string(24) "C:\Users\USERNAME\PocketMine-MP\plugins\PocketMine-DevTools.phar"
)

我尝试使用但没有用的代码

$pluginsfolder = $this->getServer()->getDataPath()."plugins/";
$plugins = array_slice(scandir($pluginsfolder), 2);
foreach ($plugins as $plugin);
$pluginspath = $pluginsfolder . $plugin;
var_dump($pluginspath);
4

1 回答 1

0

您可以使用参考修改每个元素&

foreach($plugins as &$plugin) {
    $plugin = $pluginsfolder . $plugin;
}

或者用钥匙:

foreach($plugins as $key => $plugin) {
    $plugins[$key] = $pluginsfolder . $plugin;
}

或者没有foreach

$plugins = array_map(function($plugin) use($pluginsfolder) {
                         return $pluginsfolder.$plugin
                     }, $plugins);

但是,glob()将返回完整路径:

$plugins = glob("$pluginsfolder*")
于 2017-09-11T20:36:48.753 回答