【问题标题】:Output Multi Array to CSV将多数组输出到 CSV
【发布时间】:2017-07-22 08:40:19
【问题描述】:

我想在 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');

感谢您的帮助! 编辑:添加了拧紧的 csv 导出结果的图像 编辑:更新代码

【问题讨论】:

  • 如果您将" 用于"a","b","c" 之类的字段,那么您可以转义数组值中的所有"。也做得更好fputcsv($fp, array_values($fields));
  • 在 CSV 中是第一个 <pre>Arraythat 来自 print '<pre>';printr_r($array); 为什么在 CSV 中?在看到description 的内容后,您最好使用 XML 而不是 CSV(或者您必须删除所有换行符!!!才能使 csv 正常工作)。复杂而棘手的东西。希望您现在 100% 了解 CSV 文件是如何工作的。 :-)
  • 市场迫使我使用 csv 上传,我希望它尽可能动态。我修复了所有的 " 就像你说的那样。数组括号现在不见了,但我仍然缺少清理后的列外观 :)
  • 那是因为内容中的换行符,在 CSV 本身中被解释为真正的换行符而不是内容的换行符。 CSV 文件中的换行符是如何工作的。我认为给定的数据对于 CSV 格式来说太复杂了。但是,无论如何,在添加之前从数组的值中删除所有换行符\n\r\nstr_replace(["\r\n","\n"],' ',$value);
  • 希望您的 UMLAUT 不会有任何问题。

标签: php arrays csv multidimensional-array export-to-csv


【解决方案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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-21
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 2017-08-12
    • 1970-01-01
    相关资源
    最近更新 更多