我有非常复杂的数组,我想获得HTML创建的值。
我在我的数组中有一个目录中的jpg文件列表,但是我希望所有来自'materialy-do-plis'目录的文件都有子目录和文件,有时甚至更多子文件包含更多文件。
我想在foreach数组中输入一个包含目录名和文件的精美网址。
这是我的代码,如果有帮助的话:
function pathToArray($path , $separator = '/') {
if (($pos = strpos($path, $separator)) === false) {
return array($path);
}
return array(substr($path, 0, $pos) => pathToArray(substr($path, $pos + 1)));
}
$dir = APPPATH.'../media/multimedia/obrazy/materialy-do-plis/';
$results = array();
if (is_dir($dir)) {
$iterator = new RecursiveDirectoryIterator($dir);
foreach ( new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file ) {
if ($file->isFile()) {
$thispath = str_replace('\\', '/', $file);
$thisfile = utf8_encode($file->getFilename());
$results = array_merge_recursive($results, pathToArray($thispath));
}
}
}
echo "<pre>";
print_r($results);
array_walk($products, function ($value, $key) use ($stores, &$array) {
$array[$value['store']][] = $value['product'];
});
print_r($array);
答案 0 :(得分:0)
基本上我认为,你需要一个递归函数。 此函数可以写入结果数组,该数组可以是类变量,也可以通过引用传入。
基本上就像这样(未经测试,但想要了解):
class Foo {
private $results = array();
function recurse($inputArray, $path) {
foreach($inputArray as $i => $item) {
if(is_dir($path."/".$i)) {
$this->recurse($item, $path."/".$i);
}elseif(is_file($path."/".$item)){
$this->results[] = $path."/".$item;
}
}
}
}