doujunchi1238 2013-10-22 15:43
浏览 136
已采纳

如何合并未知数量的数组

I'm new to php and after researching I haven't found an answer I understand. I am using php with html. I am reading in from text files (file_get_contents). I am doing a restaurant menu where the first item is "burgers" (burgers.txt). Second is "chicken", etc. I am separating these text files using with explode function. Here is my code

<?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("
", 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>';
}}

?>

The above code is working fine printing the tables one after the other. My problem is I need to make it dynamic. The job requires using only one screenful as it is a display menu. It needs to be dynamic in the way that if a font is a certain size I only want for example 10 items on a page. Items 1-10 on first page, 11-20 on second, etc. The sizing calculations are no problem but as can be seen from my code I am looping through the text files. I need to do a page break or similar after a certain number of items. If for example I have 6 items on first txt file and 8 on the second, I would need a page break 4 items into my second file.

The only way I can think of to do it would be a foreach merge_recursive loop such as this

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

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

This will merge arrays and assign a key to all of them allowing me to do page breaks after say 10 items in my $newArray. Problem is I don't know how many text files I will be reading from so I cannot hard code it as it's done above. Is there a way of merging all my arrays through some kind of loop where one array gets added to the one before?

As I said I'm a beginner and I have next to no help so any help would be appreciated. I am aware that the above code is also very poor so suggestions of better ways of doing it may also be helpful. Apologies for the long winded question. Go easy on me.

  • 写回答

2条回答 默认 最新

  • dongsi7067 2013-10-22 16:49
    关注

    You can merge all arrays, ie:

    $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("
    ", file_get_contents("txtfiles/".$files[$f])));
    }
    

    in the next step you can chunk $allData array into pieces

    foreach (array_chunk($allData, 10) as $chunk) {
        foreach($chunk as $singlePosition) {
         //echo
        }
        echo 'separator';
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题