只将 foreach 循环中的第一个 X 输出写入文件(在关联数组内)?
Write only the first X outputs in foreach loop to file (inside associative array)?
我有一个关联数组,可以将数据提取到评论 table 模板中。
这是位于页面底部的 "secondary table",只有 X 个项目适合。我试图只将前 X 项写入文件。
复选框正在使用 selection[] 生成 $selections。
$result = array();
foreach( array_rand($selections, count($selections)) as $k ) {
include dirname(__FILE__) . "/bottomfeature.php";
}
请注意,此循环还使用了其他文件,而不仅仅是 bottomfeature.php。
在 bottomfeature.php 的内部,我 assemble 例如功能标题(和其他元素,但仅作为示例:
$featuretitle = $result[] = $selections[$k];
$featurearray = array(
"<tr>",
"<td>$featuretitle</td>",
"</tr>"
);
$feature = implode("\n",$featurearray);
$fp = fopen("bottomfeature.txt", "a");
fwrite($fp, $feature);
fclose($fp);
本页底部仅够 4 个输出。怎样才能在4号停止执行呢?
另外,只写第一个输出怎么样?我尝试在 fopen 中使用 "w"。
$fp = fopen("bottomfeature.txt", "w");
但它只写循环的最后一项而不是第一项。
感谢您的帮助。
已更新 html 个复选框
<?php
if(isset($_REQUEST['submit'])) {
$result = array();
foreach( array_rand($selections, count($selections)) as $k ) {
include dirname(__FILE__) . "/bottomfeature.php";
}
}
?>
<form action="" method="post">
<input type="checkbox" name="selection[]" value="Brand1">Brand1</label>
<input type="checkbox" name="selection[]" value="Brand2">Brand2</label>
<input type="checkbox" name="selection[]" value="Brand3">Brand3</label>
<input type="checkbox" name="selection[]" value="Brand4">Brand4</label>
<input type="submit" name="submit" value="Save Setup" />
</form>
您只需使用索引就可以在 foreach 循环中访问从 0 开始的计数器 -- 这样您就不必手动递增计数器了。
include 可以在其条件块中引用计数器和限制来确定是否应更新文件。其他进程可以 运行 所有提交的值。
$limit = 4;
$selections = ['a', 'b', 'c', 'd', 'e'];
shuffle($selections);
foreach ($selections as $i => $selection) {
echo "before include $selection\n";
// start include code...
if ($i < $limit) {
echo "**display limited $selection**\n";
}
// end include code
echo "after include $selection\n";
echo "---\n";
}
输出:
before include d
**display limited d**
after include d
---
before include b
**display limited b**
after include b
---
before include a
**display limited a**
after include a
---
before include c
**display limited c**
after include c
---
before include e
after include e
---
我会将我的其他评论转移到我的答案中,以便我 csn 删除它们...
这没有多大意义:
$featurearray = array(
"<tr>",
"<td>$featuretitle</td>",
"</tr>"
);
$feature = implode("\n",$featurearray);
因为 $featurearray
在每次迭代中都被声明为单个元素数组(它不断被覆盖),所以更好的数据类型是 string
而不是 array
。因为只有一个元素,所以从不使用 \n
胶水。
我不建议对您的文件进行重复修改。更好/更有效的过程是准备新数据,然后在循环完成后将其写入文件。我建议重新设计您的脚本。
我有一个关联数组,可以将数据提取到评论 table 模板中。
这是位于页面底部的 "secondary table",只有 X 个项目适合。我试图只将前 X 项写入文件。
复选框正在使用 selection[] 生成 $selections。
$result = array();
foreach( array_rand($selections, count($selections)) as $k ) {
include dirname(__FILE__) . "/bottomfeature.php";
}
请注意,此循环还使用了其他文件,而不仅仅是 bottomfeature.php。
在 bottomfeature.php 的内部,我 assemble 例如功能标题(和其他元素,但仅作为示例:
$featuretitle = $result[] = $selections[$k];
$featurearray = array(
"<tr>",
"<td>$featuretitle</td>",
"</tr>"
);
$feature = implode("\n",$featurearray);
$fp = fopen("bottomfeature.txt", "a");
fwrite($fp, $feature);
fclose($fp);
本页底部仅够 4 个输出。怎样才能在4号停止执行呢?
另外,只写第一个输出怎么样?我尝试在 fopen 中使用 "w"。
$fp = fopen("bottomfeature.txt", "w");
但它只写循环的最后一项而不是第一项。
感谢您的帮助。
已更新 html 个复选框
<?php
if(isset($_REQUEST['submit'])) {
$result = array();
foreach( array_rand($selections, count($selections)) as $k ) {
include dirname(__FILE__) . "/bottomfeature.php";
}
}
?>
<form action="" method="post">
<input type="checkbox" name="selection[]" value="Brand1">Brand1</label>
<input type="checkbox" name="selection[]" value="Brand2">Brand2</label>
<input type="checkbox" name="selection[]" value="Brand3">Brand3</label>
<input type="checkbox" name="selection[]" value="Brand4">Brand4</label>
<input type="submit" name="submit" value="Save Setup" />
</form>
您只需使用索引就可以在 foreach 循环中访问从 0 开始的计数器 -- 这样您就不必手动递增计数器了。
include 可以在其条件块中引用计数器和限制来确定是否应更新文件。其他进程可以 运行 所有提交的值。
$limit = 4;
$selections = ['a', 'b', 'c', 'd', 'e'];
shuffle($selections);
foreach ($selections as $i => $selection) {
echo "before include $selection\n";
// start include code...
if ($i < $limit) {
echo "**display limited $selection**\n";
}
// end include code
echo "after include $selection\n";
echo "---\n";
}
输出:
before include d
**display limited d**
after include d
---
before include b
**display limited b**
after include b
---
before include a
**display limited a**
after include a
---
before include c
**display limited c**
after include c
---
before include e
after include e
---
我会将我的其他评论转移到我的答案中,以便我 csn 删除它们...
这没有多大意义:
$featurearray = array(
"<tr>",
"<td>$featuretitle</td>",
"</tr>"
);
$feature = implode("\n",$featurearray);
因为 $featurearray
在每次迭代中都被声明为单个元素数组(它不断被覆盖),所以更好的数据类型是 string
而不是 array
。因为只有一个元素,所以从不使用 \n
胶水。
我不建议对您的文件进行重复修改。更好/更有效的过程是准备新数据,然后在循环完成后将其写入文件。我建议重新设计您的脚本。