使用行名称转换 CSV 中的 JSON 数组?
Convert JSON Array in CSV with row name?
我不知道如何将此 JSON 数组转换为 CSV。
[relatedProducts] => Array
(
[0] => 111001
[1] => 111002
[2] => 111007
[3] => 111021
[4] => 111022
[5] => 111024
[6] => 111027
[7] => 111121
[8] => 111122
[9] => 111127
[10] => 111128
[11] => 111141
[12] => 111142
[13] => 111144
[14] => 111147
)
我希望 relatedProducts
成为行名,在 relatedProducts
下面的字段中应该有“111001,111002,111007.....”
relatedProducts
111001,111002,111007
我试过的是这样的:
foreach($array as $obj)
{
$related = $obj->relatedProducts;
};
if (empty($firstLineKeys))
{
$firstLineKeys = array_keys($relatedProducts);
fputcsv($f, $firstLineKeys, ';');
$firstLineKeys = array_flip($firstLineKeys);
}
fputcsv($f, $relatedProducts, ';');
这将按照您的要求进行,提供的数据和上下文有限:
foreach ($array as $k => $v)
{
print("$k\n".implode(',', $v)."\n");
}
产出
relatedProducts
111001,111002,111007,111021,111022,111024,111027,111121,111122,111127,111128,111141,111142,111144,111147
我不知道如何将此 JSON 数组转换为 CSV。
[relatedProducts] => Array
(
[0] => 111001
[1] => 111002
[2] => 111007
[3] => 111021
[4] => 111022
[5] => 111024
[6] => 111027
[7] => 111121
[8] => 111122
[9] => 111127
[10] => 111128
[11] => 111141
[12] => 111142
[13] => 111144
[14] => 111147
)
我希望 relatedProducts
成为行名,在 relatedProducts
下面的字段中应该有“111001,111002,111007.....”
relatedProducts
111001,111002,111007
我试过的是这样的:
foreach($array as $obj)
{
$related = $obj->relatedProducts;
};
if (empty($firstLineKeys))
{
$firstLineKeys = array_keys($relatedProducts);
fputcsv($f, $firstLineKeys, ';');
$firstLineKeys = array_flip($firstLineKeys);
}
fputcsv($f, $relatedProducts, ';');
这将按照您的要求进行,提供的数据和上下文有限:
foreach ($array as $k => $v)
{
print("$k\n".implode(',', $v)."\n");
}
产出
relatedProducts 111001,111002,111007,111021,111022,111024,111027,111121,111122,111127,111128,111141,111142,111144,111147