我有一个可变长度数组(xml),我想从中提取细节,然后将每个元素的内容一次写入一个包含25个元素的文件,直到所有元素都被处理完毕。 我对阅读和写作部分没问题,但我对于如何正确地将其拆分成块感到空白。
foreach ($elements as $element) {
$dataToWrite = $dataToWrite . $element;
}
fwrite($file, $dataToWrite)
但我如何一次迭代25个元素? 我一直在尝试使用简单的代码(下面),但这似乎是一个" bodge job"对我来说。有更清洁,更强大的解决方案吗?
<?php
$index= 0;
$colours=array("red","green","blue","yellow","brown","pink","orange","light blue","black","white","purple","mauve","cyan","brown","indigo","scarlet");
$temp = array_chunk($colours, 2, true);
$count = count($temp);
while ($index<$count){
$dataToWrite = "";
foreach ($temp[$index] as $element){
$dataToWrite = $dataToWrite . $element;
};
$index++;
echo "header " . $dataToWrite . " footer" . "<br>";
};
?>
答案 0 :(得分:1)
我会按照以下方式使用原始代码块: