English 中文(简体)
如何在拉拉维尔·布莱德模板中展示代表多重结构的阵列?
原标题:How to display an array representing a folder structure in Laravel Blade templates?

我要说的是,我有一系列的PHP阵列,代表了像以下那样的多重结构:

array:5 [▼
  0 => array:2 [▼
    "size" => "9.8 MiB"
    "name" => "Game.exe"
  ]
  1 => array:2 [▼
    "size" => "3.9 MiB"
    "name" => "Launcher.exe"
  ]
  2 => array:2 [▼
    "size" => "2 MiB"
    "name" => "USRDIR/mask.mpk"
  ]
  3 => array:2 [▼
    "size" => "1 MiB"
    "name" => "USRDIR/subs.hrt"
  ]
  4 => array:2 [▼
    "size" => "10 MiB"
    "name" => "USRDIR/fullhd/footage.mkv"
  ]

我愿利用无顺序排列的名单,以超文本形式展示这一信息,例如:

    <ul class="root-folder">
        <ul class="folder"><span>FOLDERNAME</span>
            <ul class="folder"><span>FOLDERNAME</span>
                <li class="file">FILENAME</li>
            </ul>
            <li class="file">FILENAME</li>
            <li class="file">FILENAME</li>
        <ul>
        <li class="file">FILENAME</li>
        <li class="file">FILENAME</li>
    </ul>

前2个阵列的名值是用以下格式拼写的:“档案名称:档案集延”,意思是这些档案根植。 对于这些内容,我只能扔下一个<代码><li>元素,显示其在主编<代码><ul>内的数值。 然而,第三阵列的名称价值是“文件夹/档案集”,意思是,为了直观地反映这一点,我必须制作一个新的<代码><ul>,其中新文件载于<li>内容。 也有这样的事实,即如果这些夹中有1个以上档案,那么这些阵列将多倍地点名。 之后,最后一批文件在档案前有两倍。 我的同仁会看着什么,我是在控制器中这样做的,通过一个阵列,以显示,或者在布莱德模板中仅仅通过原来的阵列?

现在,这只是我所损失的:

Controller:

    $fileArray = [];
    foreach ($files as $key => $file) {
        array_push($fileArray, $file);
        $fileArray[$key][ size ] = formatBits($file[ size ]);
        $fileArray[$key][ name ] = explode( / , $file[ name ]);
    }

    return view( single , [
         fileArray  => $fileArray
    ]);

Blade模板:

    <ul>
        @foreach ($fileArray as $file)
            @if (count($file[ name ]) > 1)
                @foreach ($file[ name ] as $folder)
                    @if ($folder == end($file[ name ]))
                    <li>
                        <span class="file-icon">?</span>{{ $folder }}<span class="file-size">({{ $file[ size ] }})
                    </li>
                    @else 
                    <li>
                        <span class="file-icon">?</span>{{ $folder }}
                    </li>
                    @endif
                @endforeach
            @else
            <li>
                <span class="file-icon">?</span>{{ $file[ name ][0] }}<span class="file-size">({{ $file[ size ] }})
            </li>
            @endif
        @endforeach
    </ul>

这在每一次铺设线时都产生了一个<代码><li>要素,该元素与控制器上显示的原始路径名称分离。 你们应当能够看到这一问题如何成问题,因为每当提及一个双倍名称时,就会产生一个新的<li>

问题回答

我有一个简短的想法:

$files = Paths::all()->toArray();//lets say you do this to have the array you told us ready

$fileArray = [];
foreach ($files as $file) {
    if(str_contains($file[ name ],  / )){
        $split = explode( / , $file[ name ]);
        $name = last($split);
        $path = array_chunk($split, sizeof($split)-1)[0];
    }else{
        $name = $file[ name ];
        $path = [];
    }
    $temp = [[ name =>$name,  size  => formatBits($file[ size ])]]; //single value in array too to ease future headaches
    while( count($path) > 0 ){
        $temp2 = [];
        $temp2[  / .last( $path ) ] = $temp;
        array_pop( $path );
        $temp3 = $temp2;
        $temp2 = $temp;
        $temp = $temp3;
    }
    $fileArray = array_merge_recursive($fileArray,$temp);
}

return view( single , compact( fileArray ));

Here s your controller but I have no more time, sorry:) In your blade you need to check if the key is string or number. Number: value is file, containing name and size, string: value is another array of directory content, and the key is the directory name. Numbers can be the directory names but they all have a slash before. Feel free to adjust it and keep up the good work! ?





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签