php globe 数组,将目录的第一个文件与

php globe array, separate first file of directory from

我正在尝试使用 globe() 将目录中的 .php 文件获取到数组中。工作正常。 我希望第一个文件在 div 中,所有其他文件在另一个 div 中。

    <?php
    $files = glob("kalenderitems/*.php");
    $num_files = count($files);
    $i = 0;
    ?>
    <div class="kal-item-<?php echo $num_files?>-col-left">
    <?php
    foreach (glob("kalenderitems/*.php") as $filename){
    if ($i == 1) {
    ?>
    <div class="kal-item-<?php echo $num_files?> nr-<?php echo $i++ ?> equaliser">
    <?php
    include $filename
    ?>
    </div>
    <?php
    };
    } 
    ?>
    </div>
    <!-- end DIV 1 -->
    <div class="kal-item-<?php echo $num_files?>-col-right">
    <?php
    foreach (glob("kalenderitems/*.php") as $filename){
    if ($i++ > 1) {
     ?>
    <div class="kal-item-<?php echo $num_files?> nr-<?php echo $i++ ?> equaliser">
    <?php
    include $filename
    ?>
    </div>
    <?php
    };
    }    
    ?>
    </div>

数组内容 (glob("kalenderitems/*.php")) 给我: insert1.php、insert2.php、insert3.php、insert4.php

目标结构

    <div class="col-left">
      <div class="element 1">include (insert1.php)</div>
    </div>
    <div class="col-right">
      <div class="element 2">include a file</div>
      <div class="element 2">include a file</div>
      <div class="element 2">include a file</div>
    </div>    

编辑 php 文件必须包含在 div 中。提供的解决方案只能构建结构并在两者之间放置 html。

请检查内部div,我刚写的。

    <?php
    $files = glob("kalenderitems/*.php");
    $num_files = count($files);
    $first_flag = 0;
    $div_1 = "";
    $div_2 = "";
    foreach (glob("kalenderitems/*.php") as $filename)
    {
        if ($first_flag == 0) 
        {

            $div_1 = "<div class='kal-item-$num_files nr-$i++ equaliser'> include $filename </div>";
        }
        else
        {
            $div_2.= "<div class='kal-item-$num_files nr-$i++ equaliser'> include $filename </div>";
        }
        $first_flag = 1;
    }

    ?>

    <div class="kal-item-<?php echo $num_files?>-col-left">
        <?php echo $div_1; ?>
    </div>
    <div class="kal-item-<?php echo $num_files?>-col-right">
        <?php echo $div_2; ?>
    </div>

我相信您正在寻找 array_shift()。它删除并 returns 数组中的第一个元素,因此这应该实现您的目标:

<?php

$files      = glob("kalenderitems/*.php");
$num_files  = count($files);

?>
<div class="kal-item-<?php echo $num_files; ?>-col-left">
     <div class="kal-item-<?php echo $num_files; ?> nr-1 equaliser">
        <?php include(array_shift($files)); ?>
     </div>
</div>

<!-- end DIV 1 -->
<div class="kal-item-<?php echo $num_files?>-col-right">
    <?php foreach($files as $filename): ?>
        <?php include($filename); ?>
    <?php endforeach; ?>
</div>