简体   繁体   English

在数组中存储多个值并输出csv

[英]Storing multiple values in array and output csv

I am trying to setup a CSV feed for another marketplace. 我正在尝试为另一个市场设置CSV提要。 Problem is that only one set of values are stored to the array. 问题是只有一组值存储到数组中。

$data = array();

while ($loop->have_posts()) : $loop->the_post();

$product = get_product($loop->post);

$title = $product->get_title();
$link = get_permalink();
$description = strip_tags($post->post_content);
$details = $post->the_excerpt;
$categories = get_the_terms($post->ID, 'product_cat');
$sku = $product->get_sku();
$price = $product->price;
$imageinfo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
$imageurl = $imageinfo[0];
$image = preg_replace($suchmuster, '', $imageurl);

foreach ($categories as $c) {
    $category = $c->name;
}

$data += [
"ean"           => $sku,
"condition"     => "100",
"listing_price" => $price,
"minimum_price" => $price,
"amount"        => 9,
"delivery_time" => "b",
"location"      => "DE"
];


endwhile;
wp_reset_query();

echo '<pre>';
print_r($data);
echo '</pre>';

My Array now looks like this: 我的阵列现在看起来像这样:

Array
(
    [ean] => SportsBag16
    [condition] => 100
    [listing_price] => 39
    [minimum_price] => 39
    [amount] => 9
    [delivery_time] => b
    [location] => DE
)

But there should be way more entries(22). 但是应该有更多的条目(22)。

What am i doing wrong? 我究竟做错了什么? Thanks for any help. 谢谢你的帮助。

You are append the output to string , you have to make the array in while conditions, in your code it replace the previous value with new values. 您将输出追加到string,必须在while条件下创建数组,在您的代码中它将用新值替换之前的值。

  $data = array();

    while ($loop->have_posts()) : $loop->the_post();

    $product = get_product($loop->post);

    $title = $product->get_title();
    $link = get_permalink();
    $description = strip_tags($post->post_content);
    $details = $post->the_excerpt;
    $categories = get_the_terms($post->ID, 'product_cat');
    $sku = $product->get_sku();
    $price = $product->price;
    $imageinfo = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID));
    $imageurl = $imageinfo[0];
    $image = preg_replace($suchmuster, '', $imageurl);

    foreach ($categories as $c) {
        $category = $c->name;
    }

$array1 = array( 
   "ean"           => $sku,
    "condition"     => "100",
    "listing_price" => $price,
    "minimum_price" => $price,
    "amount"        => 9,
    "delivery_time" => "b",
    "location"      => "DE"
); 

    $data []= $array1;

    endwhile;
    wp_reset_query();

    echo '<pre>';
    print_r($data);
    echo '</pre>';

Your error lies in using += to append to the array. 您的错误在于使用+=附加到数组。 Use the following instead: 请改用以下内容:

$data[] = [
    "ean"           => $sku,
    "condition"     => "100",
    "listing_price" => $price,
    "minimum_price" => $price,
    "amount"        => 9,
    "delivery_time" => "b",
    "location"      => "DE"
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM