想使用FLEX在客户端显示服务器(PHP)的目录树吗?
使用PHP的RecursiveDirectoryIterator,我可以创建目录树,甚至可以使用RecursiveIteratoryIterator类将其展平,但我想创建flex的树组件能够理解的目录树结构。下面是flex理解的php中的数组结构想使用FLEX在客户端显示服务器(PHP)的目录树吗?,php,apache-flex,tree,iterator,directory,Php,Apache Flex,Tree,Iterator,Directory,使用PHP的RecursiveDirectoryIterator,我可以创建目录树,甚至可以使用RecursiveIteratoryIterator类将其展平,但我想创建flex的树组件能够理解的目录树结构。下面是flex理解的php中的数组结构 array('label'=>'rootDirectory','children'=>array(array('label'=>'subFolder1','children'=>array(array('label'=>'
array('label'=>'rootDirectory','children'=>array(array('label'=>'subFolder1','children'=>array(array('label'=>'file.jpg'))),array('label'=>'EmtptyFolder','children'=>array())));
请告诉我如何将服务器端的整个目录结构创建为上述数组格式。
提前谢谢 您可以根据需要调整此代码。这不是一个复制粘贴解决方案,因为我需要它用于不同的用例,但它至少可以让您从中实现自己的解决方案。关键在于调整遍历目录树时将调用的方法
你就是我在其他地方帮助解决同一个问题的那个人吗?@sala对你的解决方案很好奇,所以为什么不添加你的答案,不管它是否是同一个OP?@Gordon,你我之间的主要区别是返回嵌套数组,而不是输出语法。如果是同一个人,我不愿意发布答案。@salathe我能理解,但我也对它感兴趣,所以它不会只针对OP。如果你不想在这里发布,它可能会进入Wiki吗?哇,这张cd是我问题的最终解决方案,即使Zend_View_Helper_Navigation_菜单也没有扩展递归迭代器迭代器类来构建树,你是从哪里想到重写这些方法的??因为它们甚至没有@php文档。net@jason在这里、那里和任何地方阅读一些关于SPL的内容。请注意,上面的方法打印阵列,而不是在内存中实际构建阵列。你必须根据你的需要调整它,或者给@ SalaSe一个答案,这样他可以考虑提供他的版本来返回一个数组。
<?php
/**
* Prints a Directory Structure as an Nested Array
*
* This iterator can be used to wrap a RecursiveDirectoryIterator to output
* files and directories in a parseable array notation. Because the iterator
* will print the array during iteration, the output has to be buffered in
* order to be captured into a variable.
*
* <code>
* $iterator = new RecursiveDirectoryAsNestedArrayFormatter(
* new RecursiveDirectoryIterator('/path/to/a/directory'),
* RecursiveIteratorIterator::SELF_FIRST
* );
* ob_start();
* foreach($iterator as $output) echo $output;
* $output = ob_get_clean();
* </code>
*
*/
class RecursiveDirectoryAsNestedArrayFormatter extends RecursiveIteratorIterator
{
/**
* Prints one Tab Stop per current depth
* @return void
*/
protected function indent()
{
echo str_repeat("\t", $this->getDepth());
}
/**
* Prints a new array declaration
* return void
*/
public function beginIteration()
{
echo 'array(', PHP_EOL;
}
/**
* Prints a closing bracket and semicolon
* @return void
*/
public function endIteration()
{
echo ');', PHP_EOL;
}
/**
* Prints an indented subarray with key being the current directory name
* @return void
*/
public function beginChildren()
{
$this->indent();
printf(
'"%s" => array(%s',
basename(dirname($this->getInnerIterator()->key())),
PHP_EOL
);
}
/**
* Prints a closing bracket and a comma
* @return void
*/
public function endChildren()
{
echo '),', PHP_EOL;
}
/**
* Prints the indented current filename and a comma
* @return void
*/
public function current()
{
if ($this->getInnerIterator()->current()->isFile()) {
printf(
'%s"%s",%s',
str_repeat("\t", $this->getDepth() +1),
$this->getInnerIterator()->current()->getBasename(),
PHP_EOL
);
}
}
}