因此,如果我有一个类似于以下的数组:
Array
(
[0] => Array
(
[Name] => Ron
[Date] => 9/23/10
[Title] => Boss
[Activity] => Soccer
)
[1] => Array
(
[Name] => Tim
[Date] => 9/23/10
[Title] => player
[Activity] => Soccer
)
[2] => Array
(
[Name] => Jim
[Date] => 9/23/10
[Title] => manager
[Activity] => Soccer
)
[3] => Array
(
[Name] => Lud
[Date] => 9/23/10
[Title] => employee
[Activity] => Soccer
)
[4] => Array
(
[Name] => Ron
[Date] => 9/23/10
[Title] => sub boss
[Activity] => Soccer
)
[5] => Array
(
[Name] => Lev
[Date] => 9/23/10
[Title] => mini boss
[Activity] => Soccer
)
[6] => Array
(
[Name] => Timpthy
[Date] => 9/23/10
[Title] => super boss
[Activity] => Soccer
)
[7] => Array
(
[Name] => Jimbo
[Date] => 9/23/10
[Title] => final battle
[Activity] => Soccer
)
[8] => Array
(
[Name] => El
[Date] => 9/23/10
[Title] => link
[Activity] => Soccer
)
)
但是 MASSIVE,我想遍历每个数组元素并以这种方式格式化数据:
Name|Date|||||Title||||||||||Activity|||||||||||||||
Name|Date|||||Title||||||||||Activity|||||||||||||||
Name|Date|||||Title||||||||||Activity|||||||||||||||
Name|Date|||||Title||||||||||Activity|||||||||||||||
Name|Date|||||Title||||||||||Activity|||||||||||||||
(基本上是 CSV,但有多个不同的分隔符)
然后我想将这些字符串写入 .txt 文件,最有效的方法是什么?一个简单的 foreach 循环可以解决问题吗?
另外,我很难找到一种方法来处理每组唯一的值到一个字符串 - 所以我很难弄清楚如何得到
[0] => Array
(
[Name] => Ron
[Date] => 9/23/10
[Title] => Boss
[Activity] => Soccer
)
[1] => Array
(
[Name] => Tim
[Date] => 9/23/10
[Title] => player
[Activity] => Soccer
)
输出
Ron|9/23/10|||||Boss||||||||||Soccer|||||||||||||||
Tim|9/23/10|||||player||||||||||Soccer|||||||||||||||
这是我最好的猜测:
$finalstring;
$tempstring;
foreach($Array as $key)
{
$i = 0;
foreach($Array as $value)
{
$i+= 1;
if($i == 1)
{
$tempstring . $value. "|";
}
if($i == 2)
{
$tempstring . $value . "|||||";
}
if($i == 3)
{
$tempstring . $value . "||||||||||";
}
if($i == 4)
{
$tempstring . $value . "|||||||||||||||||";
}
}
$finalstring . $tempstring . "/n";
}
$fh = fopen("text.txt", "w");
fwrite($fh, $finalstring);
是否有一个简单的函数可以让我将每个值集格式化为预先格式化的字符串,然后有效地将这些字符串写入文件?