【发布时间】:2020-10-24 03:38:29
【问题描述】:
我知道这个问题的答案是显而易见的,但我在过去 3 天里一直在试图弄清楚。我无法将多维数组导出到导出的 .csv 文件中的正确布局中。
我似乎能够获得所有数据但布局不正确,或者我可以获得正确布局但不是所有数据。
这是数组
array (size=106)
0 =>
array (size=6)
0 => string 'Title' (length=5)
1 => string 'image_url' (length=9)
3 => string 'SKU CODE' (length=8)
4 => string 'TITLE SIZE' (length=10)
5 => string 'DESCRIPTION' (length=11)
6 => string 'BASE SKU' (length=8)
1 =>
array (size=6)
0 => string 'A witch and her cat live here' (length=29)
1 => string 'https://beautifulhomegifts.com/a-witch-and-her-cat-live-here/' (length=61)
3 =>
array (size=4)
0 => string 'BHG-MS-AWAHCLH030720' (length=20)
1 => string 'BHG-MS-AWAHCLH030720-A5' (length=23)
2 => string 'BHG-MS-AWAHCLH030720-A4' (length=23)
3 => string 'BHG-MS-AWAHCLH030720-A3' (length=23)
4 =>
array (size=4)
0 => string 'A witch and her cat live here' (length=29)
1 => string 'A witch and her cat live here - 150mm x 200mm' (length=45)
2 => string 'A witch and her cat live here - 201mm x 305mm' (length=45)
3 => string 'A witch and her cat live here - 305mm x 400mm' (length=45)
5 =>
array (size=4)
0 => string 'A witch and her cat live here' (length=29)
1 => string 'A witch and her cat live here' (length=29)
2 => string 'A witch and her cat live here' (length=29)
3 => string 'A witch and her cat live here' (length=29)
6 =>
array (size=3)
1 => string 'BHG-MS-AWAHCLH030720' (length=20)
2 => string 'BHG-MS-AWAHCLH030720' (length=20)
3 => string 'BHG-MS-AWAHCLH030720' (length=20)
2 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
3 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
4 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
5 =>
array (size=6)
0 => string 'Autism House Rules' (length=18)
1 => string 'https://beautifulhomegifts.com/autism-house-rules/' (length=50)
3 =>
array (size=4)
0 => string 'BHG-MS-AHR030720' (length=16)
1 => string 'BHG-MS-AHR030720-A5' (length=19)
2 => string 'BHG-MS-AHR030720-A4' (length=19)
3 => string 'BHG-MS-AHR030720-A3' (length=19)
4 =>
array (size=4)
0 => string 'Autism House Rules' (length=18)
1 => string 'Autism House Rules - 150mm x 200mm' (length=34)
2 => string 'Autism House Rules - 201mm x 305mm' (length=34)
3 => string 'Autism House Rules - 305mm x 400mm' (length=34)
5 =>
array (size=4)
0 => string 'Autism House Rules' (length=18)
1 => string 'Autism House Rules' (length=18)
2 => string 'Autism House Rules' (length=18)
3 => string 'Autism House Rules' (length=18)
6 =>
array (size=3)
1 => string 'BHG-MS-AHR030720' (length=16)
2 => string 'BHG-MS-AHR030720' (length=16)
3 => string 'BHG-MS-AHR030720' (length=16)
6 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
7 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
8 =>
array (size=2)
0 => string '' (length=0)
1 => string '' (length=0)
9 =>
我已经尝试了多种方法来让它工作,这是我最接近正确的方法
$f = fopen('new.csv', 'a'); // Configure fOpen to create, open and write only.
if ($f != false){
// Loop over the array and passing in the values only.
foreach ($the_big_array as $row){
fputcsv($f, $row);
}
}
fclose($f);
这给了我这个布局,但它只是显示有一个子数组并且不输出子数组的数据。
Above 是我得到的输出。
下面是我想要实现的布局。
我还在 foreach 循环中尝试了一个 foreach 循环来获取数据,当我这样做时,我得到了所有数据,但不是在同一个布局中。我浏览了这里的所有帖子,很多帖子都接近我想要实现的目标,但没有一个给出正确的布局。
总而言之,我想将 $the_big_array 导出到一个 .csv 文件,该文件具有电子表格中 .csv 的第二个图像的布局。谢谢
array (
0 =>
array (
0 => 'Title',
1 => 'image_url',
3 => 'SKU CODE',
4 => 'TITLE SIZE',
5 => 'DESCRIPTION',
6 => 'BASE SKU',
),
1 =>
array (
0 => 'A witch and her cat live here',
1 => 'https://beautifulhomegifts.com/a-witch-and-her-cat-live-here/',
3 =>
array (
0 => 'BHG-MS-AWAHCLH030720',
1 => 'BHG-MS-AWAHCLH030720-A5',
2 => 'BHG-MS-AWAHCLH030720-A4',
3 => 'BHG-MS-AWAHCLH030720-A3',
),
4 =>
array (
0 => 'A witch and her cat live here',
1 => 'A witch and her cat live here - 150mm x 200mm',
2 => 'A witch and her cat live here - 201mm x 305mm',
3 => 'A witch and her cat live here - 305mm x 400mm',
),
5 =>
array (
0 => 'A witch and her cat live here',
1 => 'A witch and her cat live here',
2 => 'A witch and her cat live here',
3 => 'A witch and her cat live here',
),
6 =>
array (
1 => 'BHG-MS-AWAHCLH030720',
2 => 'BHG-MS-AWAHCLH030720',
3 => 'BHG-MS-AWAHCLH030720',
),
),
2 =>
array (
0 => '',
1 => '',
),
3 =>
array (
0 => '',
1 => '',
),
4 =>
array (
0 => '',
1 => '',
),
5 =>
array (
0 => 'Autism House Rules',
1 => 'https://beautifulhomegifts.com/autism-house-rules/',
3 =>
array (
0 => 'BHG-MS-AHR030720',
1 => 'BHG-MS-AHR030720-A5',
2 => 'BHG-MS-AHR030720-A4',
3 => 'BHG-MS-AHR030720-A3',
),
4 =>
array (
0 => 'Autism House Rules',
1 => 'Autism House Rules - 150mm x 200mm',
2 => 'Autism House Rules - 201mm x 305mm',
3 => 'Autism House Rules - 305mm x 400mm',
),
5 =>
array (
0 => 'Autism House Rules',
1 => 'Autism House Rules',
2 => 'Autism House Rules',
3 => 'Autism House Rules',
),
6 =>
array (
1 => 'BHG-MS-AHR030720',
2 => 'BHG-MS-AHR030720',
3 => 'BHG-MS-AHR030720',
),
),
【问题讨论】:
-
所以忽略所有空的?
-
你可以显示你的数组的
var_export()吗? -
为什么不想为每一行复制标题字段?您想要的输出不能以任何有意义的方式排序或过滤。另外,数组中的空行是怎么回事?它们是应该插入的 CSV 行的占位符吗?你能改变数组来过滤掉它们吗?
-
空的可以忽略,只要布局和第二张图一样
-
空值是 CSV 行的占位符
标签: php arrays csv multidimensional-array fputcsv