将多数组输出到 CSV
Output Multi Array to CSV
我想在 CSV 文件中写入以下数组:
Array ( [0] => Array
(
[ean] => SportsBag1
[condition] => 100
[listing_price] => 39
[minimum_price] => 39
[amount] => 26
)
[1] => Array
(
[ean] => SportsBag2
[condition] => 100
[listing_price] => 37
[minimum_price] => 37
[amount] => 17
)
and so on...
CSV 应如下所示:
ean | condition | listing_price | minimum_price | amount
val | value | value | value | value
val | value | value | value | value
val | value | value | value | value
现在所有内容都在一列中,整个数组看起来(带有括号和其他所有内容)而不是他自己的列中的每个值(纯数据)
Whosebug 上有很多线程针对这个主题,但没有什么能真正解决我的问题。
我正在使用以下代码:
//EDIT
$suchmuster = array();
$suchmuster[0] = '/ /';
$suchmuster[1] = '/\s\s+/';
$suchmuster[2] = '/-150x150/';
$suchmuster[3] = '/-300x300/';
$suchmuster[4] = '/-500x500/';
$inventorydata = array();
$productdata = array();
while ($loop->have_posts()) : $loop->the_post();
$product = get_product($loop->post);
$title = $product->get_title();
$link = get_permalink();
$descriptionunstripped = strip_tags($post->post_content); //EDIT
$description = preg_replace($suchmuster, ' ', $descriptionunstripped); //EDIT
$details = $post->the_excerpt;
$categories = get_the_terms($post->ID, 'product_cat');
$sku = $product->get_sku();
$stock = $product->get_stock_quantity();
$price = $product->price;
$imageinfo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
$imageurl = $imageinfo[0]; // 0 ist die URL
$image = preg_replace($suchmuster, '', $imageurl);
$attachment_ids = $product->get_gallery_attachment_ids();
$count = 0;
foreach( $attachment_ids as $attachment_id )
{
if($count > 12){
// echo 'Nächste Produkt';
break;
}
${'bild'.$count} = wp_get_attachment_url( $attachment_id );
$count += 1;
}
foreach ($categories as $c) {
$category = $c->name;
}
$inventorydata[] = [
"ean" => $sku,
"condition" => "100",
"listing_price" => $price,
"minimum_price" => $price,
"amount" => $stock,
"delivery_time" => "b",
"location" => "DE"
];
$productdata[] = [
"ean" => $sku,
"title" => $title,
"short_description" => $details,
"description" => $description,
"manufacturer" => 'Manufaktur13',
"mpn" => '',
"picture1" => $bild0,
"picture2" => $bild1,
"picture3" => $bild2,
"picture4" => $bild3,
"picture5" => $bild4,
"picture6" => $bild5,
"picture7" => $bild6,
"picture8" => $bild7,
"picture9" => $bild8,
"picture10" => $bild9,
"picture11" => $bild10,
"picture12" => $bild11,
"colour" => '',
"target" => 'Unisex',
"shoe_size" => ''
];
endwhile;
wp_reset_query();
$fp = fopen('file.csv', 'w');
foreach ($productdata as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
header("Content-Type: text/csv");
header('Content-Disposition: attachment;filename=file.csv');
感谢您的帮助!
编辑:添加了 screwed csv 导出结果的图像
编辑:更新代码
我不确定这个问题的状态,它看起来更像是一个正在进行的项目,而不是一个简单的问题。总之,我想帮你解决这个问题。
您的 $image = preg_replace($suchmuster, '', $imageurl);
不会循环遍历您的正则表达式模式数组。您可以将所有要替换的匹配项通过管道传输到一个正则表达式模式中,如下所示:
$image=preg_replace('( |\s\s+|-150x150|-300x300|-500x500)','',$imageurl);
或者,如果您希望以后在管理代码方面有更好的灵活性,您可以这样做:
$patterns=array(" ","\s\s+","-150x150","-300x300","-500x500");
$image=preg_replace('/('.implode('|',$patterns).')/','',$imageurl);
我想在 CSV 文件中写入以下数组:
Array ( [0] => Array
(
[ean] => SportsBag1
[condition] => 100
[listing_price] => 39
[minimum_price] => 39
[amount] => 26
)
[1] => Array
(
[ean] => SportsBag2
[condition] => 100
[listing_price] => 37
[minimum_price] => 37
[amount] => 17
)
and so on...
CSV 应如下所示:
ean | condition | listing_price | minimum_price | amount
val | value | value | value | value
val | value | value | value | value
val | value | value | value | value
现在所有内容都在一列中,整个数组看起来(带有括号和其他所有内容)而不是他自己的列中的每个值(纯数据) Whosebug 上有很多线程针对这个主题,但没有什么能真正解决我的问题。
我正在使用以下代码:
//EDIT
$suchmuster = array();
$suchmuster[0] = '/ /';
$suchmuster[1] = '/\s\s+/';
$suchmuster[2] = '/-150x150/';
$suchmuster[3] = '/-300x300/';
$suchmuster[4] = '/-500x500/';
$inventorydata = array();
$productdata = array();
while ($loop->have_posts()) : $loop->the_post();
$product = get_product($loop->post);
$title = $product->get_title();
$link = get_permalink();
$descriptionunstripped = strip_tags($post->post_content); //EDIT
$description = preg_replace($suchmuster, ' ', $descriptionunstripped); //EDIT
$details = $post->the_excerpt;
$categories = get_the_terms($post->ID, 'product_cat');
$sku = $product->get_sku();
$stock = $product->get_stock_quantity();
$price = $product->price;
$imageinfo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
$imageurl = $imageinfo[0]; // 0 ist die URL
$image = preg_replace($suchmuster, '', $imageurl);
$attachment_ids = $product->get_gallery_attachment_ids();
$count = 0;
foreach( $attachment_ids as $attachment_id )
{
if($count > 12){
// echo 'Nächste Produkt';
break;
}
${'bild'.$count} = wp_get_attachment_url( $attachment_id );
$count += 1;
}
foreach ($categories as $c) {
$category = $c->name;
}
$inventorydata[] = [
"ean" => $sku,
"condition" => "100",
"listing_price" => $price,
"minimum_price" => $price,
"amount" => $stock,
"delivery_time" => "b",
"location" => "DE"
];
$productdata[] = [
"ean" => $sku,
"title" => $title,
"short_description" => $details,
"description" => $description,
"manufacturer" => 'Manufaktur13',
"mpn" => '',
"picture1" => $bild0,
"picture2" => $bild1,
"picture3" => $bild2,
"picture4" => $bild3,
"picture5" => $bild4,
"picture6" => $bild5,
"picture7" => $bild6,
"picture8" => $bild7,
"picture9" => $bild8,
"picture10" => $bild9,
"picture11" => $bild10,
"picture12" => $bild11,
"colour" => '',
"target" => 'Unisex',
"shoe_size" => ''
];
endwhile;
wp_reset_query();
$fp = fopen('file.csv', 'w');
foreach ($productdata as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
header("Content-Type: text/csv");
header('Content-Disposition: attachment;filename=file.csv');
感谢您的帮助!
编辑:添加了 screwed csv 导出结果的图像
编辑:更新代码
我不确定这个问题的状态,它看起来更像是一个正在进行的项目,而不是一个简单的问题。总之,我想帮你解决这个问题。
您的 $image = preg_replace($suchmuster, '', $imageurl);
不会循环遍历您的正则表达式模式数组。您可以将所有要替换的匹配项通过管道传输到一个正则表达式模式中,如下所示:
$image=preg_replace('( |\s\s+|-150x150|-300x300|-500x500)','',$imageurl);
或者,如果您希望以后在管理代码方面有更好的灵活性,您可以这样做:
$patterns=array(" ","\s\s+","-150x150","-300x300","-500x500");
$image=preg_replace('/('.implode('|',$patterns).')/','',$imageurl);