我试图更改写入我的csv的第一行,列标题的文本。我试图使用unset
,但它无效。我引用了错误的数组吗?任何帮助,将不胜感激。我的代码如下:
for ($x=0; $x<$count; $x++)
{
$currentRecord = $response['data'][$x];
$jsonDataInArray[] = array
(
"b98336b40c8420dc2f1401d6451b1b47198eee6d" => $response['data'][$x]['b98336b40c8420dc2f1401d6451b1b47198eee6d'],
"17a14a9da9815451ff5ffc669d407e8b0376b06b" => $response['data'][$x]['17a14a9da9815451ff5ffc669d407e8b0376b06b'],
"3eedd4fd08f44a72d911dc4934a6916f3b31911b" => $response['data'][$x]['3eedd4fd08f44a72d911dc4934a6916f3b31911b'],
"52ede287f6c55eb6b12821ca24f74098779abdce" => $response['data'][$x]['52ede287f6c55eb6b12821ca24f74098779abdce'],
"13916ba291ab595f27aefbff8b6c43a3fb467b72" => $response['data'][$x]['13916ba291ab595f27aefbff8b6c43a3fb467b72']
)
)
//change key names.
$jsonDataInArray["Col1"] = $jsonDataInArray["b98336b40c8420dc2f1401d6451b1b47198eee6d"];
unset($jsonDataInArray["b98336b40c8420dc2f1401d6451b1b47198eee6d"]);
//first row of data
$test_array = $jsonDataInArray[0];
//open file to write towards
if($startPos == 0){
$fp = fopen('output.csv', 'w');
fputcsv($fp, array_keys($response['data'][0]));
}else{
$fp = fopen('output.csv', 'a');
}
//write data
foreach ($jsonDataInArray as $fields)
{
fputcsv($fp, $fields);
}
我目前的输出是:
b98336b40c8420dc2f1401d6451b1b47198eee6d|17a14a9da9815451ff5ffc669d407e8b0376b06b|3eedd4fd08f44a72d911dc4934a6916f3b31911b|52ede287f6c55eb6b12821ca24f74098779abdce
4616|||stuff
我想要的输出是
Col1|17a14a9da9815451ff5ffc669d407e8b0376b06b|3eedd4fd08f44a72d911dc4934a6916f3b31911b|52ede287f6c55eb6b12821ca24f74098779abdce
4616|||stuff
答案 0 :(得分:2)
您的$jsonDataInArray
包含数字索引,每个索引都包含一个数组。
替换
$jsonDataInArray["Col1"] = $jsonDataInArray["b98336b40c8420dc2f1401d6451b1b47198eee6d"];
unset($jsonDataInArray["b98336b40c8420dc2f1401d6451b1b47198eee6d"]);
与
$jsonDataInArray[0]["Col1"] = $jsonDataInArray[0]["b98336b40c8420dc2f1401d6451b1b47198eee6d"];
unset($jsonDataInArray[0]["b98336b40c8420dc2f1401d6451b1b47198eee6d"]);
让它在您的测试中有效,但您可能希望将其替换为循环,以便为每个条目更改它。
答案 1 :(得分:1)
您正在输出错误的数组
更改此部分以使用$data = model::find()
->andWhere('plz = :plz',[':plz' => 40])
->andWhere('plz = :plz',[':plz' => 40000])
->all();
而不是$jsonDataInArray
$response