简体   繁体   中英

Laravel problem with @foreach loop in blade view

From Ziparchive want name and size of files in Zip file. Problem for me is how can add name and size to table in blade view.

controller :

    $ziparchive = new \ZipArchive(); 
    $ziparchive->open($fileName);
    $filenames = [];
    $filesizes = [];
    if(!empty($ziparchive)){         
        for( $i = 0; $i < $ziparchive->numFiles; $i++ ){ 
            $stat = $ziparchive->statIndex( $i ); 
            $filenames[]= basename( $stat['name'] ) . PHP_EOL;
            $filesizes[]= basename( $stat['size'] );

    return view('view',["filenames"=>$filenames,"filesizes"=>$filesizes]);

dd($filenames, $filesizes) return two arrays:

 1.png
 2.png
 3.png
 4.png

 10
 12
 13
 796

in view.blade.php

@foreach($filenames as $filename )
    @foreach($filesizes as $filesize)
        <tr>
            <td>{{$filename}}</td>
            <td>{{$filesize}}</td>
        </tr>
    @endforeach
@endforeach

return in table this:

 1.png 10
 1.png 12
 1.png 13
 1.png 796
 2.png 10
 2.png 12
 2.png 13
 ....

what do i missing ?

try this

@foreach($filenames as $key => $filename )
    <tr>
        <td>{{ $filename }}</td>
        <td>{{ $filesizes[$key] ?? "" }}</td>
    </tr>
@endforeach

as both $filenames and $filesizes same index have same value so you can use index to get value from $filesizes[index]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM