繁体   English   中英

如何合并未知数量的数组

[英]How to merge an unknown number of arrays

我是php的新手,经过研究,我找不到我理解的答案。 我正在使用PHP与HTML。 我正在从文本文件(file_get_contents)读入。 我正在做一个餐厅菜单,第一项是“汉堡”(burgers.txt)。 第二个是“鸡”等。我使用explode功能分离这些文本文件。 这是我的代码

<?php

$dir = 'C:\xampp\htdocs\posplus\txtfiles';
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;//array of files
}
$dot = array_shift($files);
$dotdot = array_shift($files);

$numfiles = count($files);//counts number of text files
for($f=0;$f<$numfiles;$f++)
{
$filearray = array();
$filearray[$f] = file("txtfiles/".$files[$f]);  //txtfile."burgers.txt"
$linessarray = array();
$linesarray[$f] = count($filearray[$f]);  //number of lines in each file (to be           displayed on different lines)
$namesarray = array();
$namesarray[$f] = pathinfo($files[$f]);  //get name of file without extension(.txt) 
${'array'.$f} = explode("\n", file_get_contents("txtfiles/".$files[$f]));

?>
</head>

<body>
<?php

echo'<table align="center">
    <tr>
    <th colspan = "3">'.$namesarray[$f]['filename'].'</th>
</tr>';
//filename without ".txt"
for ($i=0; $i<$linesarray[$f]; $i++) { //loop through each text file

$part = explode(',', ${'array'.$f}[$i]);//split each line in text file by comma example       (cheeseburger, large, €3.00, made from finest beef)
  echo'<tr>

        <td id="name">'.$part[0].'</td>

        <td id="size">'.$part[1].'</td> 

        <td id="price">'.$part[2].'</td>    

 </tr>
 <tr>
        <td id="desc" colspan="3">'.$part[3].'</td> 
 </tr>';
}}

?>

上面的代码可以一个接一个地打印表格。 我的问题是我需要让它变得动态。 作业需要只使用一个屏幕,因为它是一个显示菜单。 它需要是动态的,如果字体是一定的大小我只想要一个页面上的10个项目。 第一页上的项目1-10,第二页上的11-20,等等。大小计算没有问题,但从我的代码可以看出我循环遍历文本文件。 我需要在一定数量的项目后进行分页或类似。 例如,如果我在第一个txt文件中有6个项目而在第二个文件中有8个项目,那么我需要在第二个文件中分页4个项目。

我能想到的唯一方法是使用foreach merge_recursive循环

$newArray = array_merge_recursive($array0, $array1, $array2); //merge burgers.txt with chicken.txt, etc

foreach ($newArray as $key => $value) {
      echo "$key - $value <br />"; 
}

这将合并数组并为所有这些分配一个键,允许我在$ newArray中说出10个项目之后进行分页。 问题是我不知道我将读取多少文本文件,所以我不能像上面那样硬编码。 有没有办法通过某种循环合并我的所有数组,其中一个数组被添加到之前的数组?

正如我所说,我是一个初学者,我几乎没有任何帮助,所以任何帮助将不胜感激。 我知道上面的代码也很差,所以建议更好的方法可能也会有所帮助。 为冗长的问题道歉。 放轻松我。

您可以合并所有数组,即:

$allData = array();
for($f=0;$f<$numfiles;$f++)
{
    $filearray = array();
    $filearray[$f] = file("txtfiles/".$files[$f]);  //txtfile."burgers.txt"
    $linessarray = array();
    $linesarray[$f] = count($filearray[$f]);  //number of lines in each file (to be           displayed on different lines)
    $namesarray = array();
    $namesarray[$f] = pathinfo($files[$f]);  //get name of file without extension(.txt) 
    $allData = array_merge($allData, explode("\n", file_get_contents("txtfiles/".$files[$f])));
}

在下一步中,您可以将$ allData数组分块

foreach (array_chunk($allData, 10) as $chunk) {
    foreach($chunk as $singlePosition) {
     //echo
    }
    echo 'separator';
} 

谢谢你的帮助。 你的信息非常有用。 但是,在使用这种方法时,如果我在第一个文本文件中说了15行,它将在第10行之后和第15行之后再次分开,在15(16-25)之后开始下一个块。 理想情况下,无论文本文件的大小如何,我都需要它来显示0-10,10-20,20-30行。 换句话说,能够在同一块10中显示多个文本文件的内容。也许我错过了一些明显的东西,但我无法弄清楚如何修复它。 这是我的代码,我使用div来分隔块

<?php

$dir = 'C:\xampp\htdocs\posplus\txtfiles';
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
  $files[] = $filename;//array of files
}
$dot = array_shift($files);
$dotdot = array_shift($files);

$numfiles = count($files);//counts number of text files
for($f=0;$f<$numfiles;$f++)
{
$filearray = array();
$filearray[$f] = file("txtfiles/".$files[$f]);
$numlinesarray = array();
$numlinesarray[$f] = count($filearray[$f]);//number of lines in each file (to be displayed on     different lines)

$namesarray = array();
$namesarray[$f] = pathinfo($files[$f]);//get name of file without extension(.txt) 
$allData = array();
$allData = array_merge($allData, explode("\n", file_get_contents("txtfiles/".$files[$f]))); 
?>
</head>

<body>
<div id = "container">

<?php
foreach (array_chunk($allData, 21) as $chunk) {

echo'<div class ="col1"><table align="center">
<tr>
    <th colspan = "3">'.$namesarray[$f]['filename'].'</th>
</tr>';
foreach($chunk as $singlePosition) {


    $part = explode(',', $singlePosition);

    echo'<tr>
             <td id="name">'.$part[0].'</td>
             <td id="size">'.$part[1].'</td>    
             <td id="price">'.$part[2].'</td>   

         </tr>';
}
echo '</table></div>';

   }    
} 
?>
</div>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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