doujing5937 2013-07-31 01:53
浏览 34
已采纳

输出多维数组,同时保持原始键集

I'm not even sure if I am writing this code as efficiently as possible but I'm stuck and need help...

HTML

<?php
    echo outputDirArray($dir_array, 0, '');
?>

In PHP, I have a function to put my directories into an array:

function dirToArray($dir) {
$result = array();
$cdir = scandir($dir);

foreach ($cdir as $key => $value) {
    if (!in_array($value,array('.','..'))) {
        if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
            $result[$value] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
        } else {
            $result[] = $value;
        }
    }
}
return $result;
} //Thanks to someone on this site for this function

I then take that array and pass it to another function to output it the way I need it to:

function OutputDirArray(&$arr, $x, $origName) {
$str;
foreach ($arr as $key => $value) {
    if($x == 0) $origName = $key;

    if (is_array($value)) {
        if($x > 0) $str .= '</div>';
        $str .= '<div class="infoDiv clearfix"><span class="title">'.$key.'</span><br/>'.OutputDirArray($value, ++$x, $origName).'</div>';
        --$x;
    } else {
        if($x > 0) {
            $str .= '<div class="displayImgs"><img src="images/'.$origName.'/'.$value.'"/></div>';
        } else {
            $str .= '<div class="infoDiv clearfix"><span class="title">Profile Picture</span>
                    <div class="displayImgs"><img src="images/'.$value.'"/></div></div>';
        }
    }
}
return $str;

}

My var_dump($dir_array); output:

array(3) { [0]=> string(7) "img1.jpg" ["test1"]=> array(4) { [0]=> string(15) "subimg1.gif" [1]=> string(9) "subimg2.png" [2]=> string(9) "subimg3.jpg" ["test3"]=> array(3) { [0]=> string(15) "subsubimg1.gif" [1]=> string(9) "subsubimg2.png" [2]=> string(9) "subsubimg3.jpg" } } ["test2"]=> array(3) { [0]=> string(15) "subimg1.gif" [1]=> string(9) "subimg2.png" [2]=> string(9) "subimg3.jpg" } } 

What am I trying to do here is take in the dir_array and walk through outputting the html code. Everything seems to work fine except for two problems...

1) If I have a subsub directory, $key in <span class="title">'.$key.'</span> outputs the correct key (in my case, test3). However $origName in <img src="images/'.$origName.'/'.$value.'"/> outputs the original key used (test1).

2) I believe because of my subsub dir problem, I am left with an extra </div> after the subsub (test3) outputs.

It's been too long now trying to figure this out, so any help is greatly appreciated.

  • 写回答

2条回答 默认 最新

  • dpvhv66448 2013-07-31 02:45
    关注

    This is very close to what you want

    If you would like me to comment it I would gladly do so.

    The code preview looks really nice.

    Just a tip, get the html correct before you start to add attributes. It only makes things more complicated.

    <?php
        function dirToArray($dir) {
            $result = array("name" => substr($dir, strrpos($dir, "\\") + 1), "children" => array());
            foreach(scandir($dir) as $key => $value) {
                if (!in_array($value,array('.', '..'))) {
                    if (is_dir($dir . DIRECTORY_SEPARATOR . $value)) {
                        $result["children"][] = dirToArray($dir . DIRECTORY_SEPARATOR . $value);
                    } else {
                        $result["children"][]["name"] = $value;
                    }
                }
            }
            return $result;
        }
    
        function OutputDirArray($arr, $origName = ".", $tab = "") {
            $str = "$tab<div>$origName</div>";
            for($i = 0; $i < count($arr["children"]); $i++) {
                $child = $arr["children"][$i];          
                $name = $child["name"];         
                $path = $origName . DIRECTORY_SEPARATOR . $name;
                if ($child["children"]) {
                    $str .= "
    $tab<div>
    " . OutputDirArray($child, $path, "$tab\t") . "
    $tab</div>";
                } else {
                    $str .= "
    $tab<div><img src=\"images\\$path\"/></div>";
                }
            }
            return $str;
        }
    
        echo outputDirArray(dirToArray("path\\to\\file"), "path\\to\\file");
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图