我想在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
现在,一列中的所有内容和整个数组看起来(带括号和其他所有内容)而不是每个值(纯数据)都在他自己的列中 在stackoverflow上有很多针对这个主题的线程,但没有什么能真正解决我的问题。
我使用以下代码:
//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');
答案 0 :(得分:1)
我不确定这个问题的状态,它似乎更像是一个正在进行的项目,而不是一个简洁的问题。无论如何,我想帮你解决这个问题。
您的$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);