我使用以下代码将我的数据数组(逗号分隔但不是从文件中)更改为可以使用的数组。我的代码如下......
public function exportPartsAuthority($fileArray)
{
// Do whatever - sample code for a webservice request below.
foreach ($fileArray as $filename => $fileContent) {
// Do nothing
}
foreach(explode("\n",$fileContent) as $line){
$item=explode(",",$line);
file_put_contents('/home/apndev/public_html/output.txt', print_r($item, true));
}
}
$ fileContent的值如下所示......
"100000002","flatrate_flatrate","1.0000","Brian","","","","","Sunrise","33323","Florida","US","","",
"100000002","flatrate_flatrate","1.0000","Brian","","","","","Sunrise","33323","Florida","US","","",
"100000003","flatrate_flatrate","1.0000","Brian","","","","","Sunrise","33323","Florida","US","2P-225","A1",
这就是爆炸$ fileContent后我的文件出现的原因......
Array
(
[0] => "100000002"
[1] => "flatrate_flatrate"
[2] => "1.0000"
[3] => "Brian"
[4] => ""
[5] => ""
[6] => ""
[7] => ""
[8] => "Sunrise"
[9] => "33323"
[10] => "Florida"
[11] => "US"
[12] => ""
[13] => ""
[14] =>
"100000002"
[15] => "flatrate_flatrate"
[16] => "1.0000"
[17] => "Brian"
[18] => ""
[19] => ""
[20] => ""
[21] => ""
[22] => "Sunrise"
[23] => "33323"
[24] => "Florida"
[25] => "US"
[26] => ""
[27] => ""
[28] =>
"100000003"
[29] => "flatrate_flatrate"
[30] => "1.0000"
[31] => "Brian"
[32] => ""
[33] => ""
[34] => ""
[35] => ""
[36] => "Sunrise"
[37] => "33323"
[38] => "Florida"
[39] => "US"
[40] => "2P-225"
[41] => "A1"
[42] =>
)
我如何将该字符串中的每一行作为自己的数组生成?
答案 0 :(得分:0)
你真的很接近,因为你已经通过调用explode()
为每一行创建了一个新数组 - 你只需要一个变量来保存所有$item
你的'重新创建,并在循环中的每次迭代中添加$item
:
function exportPartsAuthority($fileArray) {
//this would hold your output
$arrayOfArrays = array();
foreach ($fileArray as $filename => $fileContent) {
// Do nothing
foreach(explode("\n",$fileContent) as $line){
$item=explode(",",$line);
file_put_contents('/home/apndev/public_html/output.txt', print_r($item, true));
//now add it to your arrayOfArrays
$arrayOfArrays[] = $item;
}
}
}