簡體   English   中英

在數組中存儲多個值並輸出csv

[英]Storing multiple values in array and output csv

我正在嘗試為另一個市場設置CSV提要。 問題是只有一組值存儲到數組中。

$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>';

我的陣列現在看起來像這樣:

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

但是應該有更多的條目(22)。

我究竟做錯了什么? 謝謝你的幫助。

您將輸出追加到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>';

您的錯誤在於使用+=附加到數組。 請改用以下內容:

$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