【问题标题】:Storing multiple values in array and output csv在数组中存储多个值并输出 csv
【发布时间】:2017-03-02 11:46:19
【问题描述】:

我正在尝试为另一个市场设置 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)。

我做错了什么?感谢您的帮助。

【问题讨论】:

    标签: php arrays loops multidimensional-array


    【解决方案1】:

    您将输出附加到 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>';
    

    【讨论】:

    • 天哪,我知道它会这么简单。 :) 谢谢老兄!
    【解决方案2】:

    您的错误在于使用+= 附加到数组。请改用以下内容:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 2022-07-27
      • 2018-02-25
      相关资源
      最近更新 更多