gpt4 book ai didi

PHP分号分隔文件生成

转载 作者:行者123 更新时间:2023-12-04 20:42:50 25 4
gpt4 key购买 nike

我正在使用此代码生成分号分隔的文件:

for ($i = 0; $i < 3; $i = $i+1)
{
array_push($dataArray,"$dataCell1","$dataCell2","$dataCell3","$dataCell4","$dataCell5","$dataCell6","$dataCell7","$dataCell8","$dataCell9","$dataCell10","$dataCell11");

$stringData = rtrim(implode(';', $dataArray), ';'); //rtrim will prevent the last cell to be imploded by ';'

$stringData .= "\r\n";
}
我想要的是:
enter image description here
(数据用分号隔开,行用newLine隔开)
我得到的是:
enter image description here
(数据用分号分隔但不添加新行,所有数据都显示在单行中)
请告诉我我做错了什么..

最佳答案

你的问题在于你的逻辑。您在每次迭代中继续将数据添加到 $dataArray 中,而代码中的以下语句会将 stingData 设置为 $dataArray 的内爆值仅在最后一个循环迭代中 .那时, $dataArray 包含所有数据值,因为您不断向它推送 3 次迭代。

$stringData = rtrim(...)

你想要做的是:
<?php

//concatenation string
$stringData = '';

for ($i = 0; $i < 3; $i = $i+1)
{
//init the array with every iteration.
$dataArray = array();

//push your values
array_push($dataArray,"$dataCell1","$dataCell2","$dataCell3","$dataCell4","$dataCell5","$dataCell6","$dataCell7","$dataCell8","$dataCell9","$dataCell10","$dataCell11");

//concatenate
$stringData .= rtrim(implode(';', $dataArray), ';');

//new line
$stringData .= "\r\n";
}

//print
echo $stringData . "\n";
?>

关于PHP分号分隔文件生成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28809473/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com