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

时间:2017-03-02 11:46:20

标签: php arrays loops multidimensional-array

我正在尝试为其他市场设置CSV Feed。 问题是只有一组值存储在数组中。

a['1']

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

但是应该有更多的条目(22)。

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

2 个答案:

答案 0 :(得分:1)

您将输出附加到字符串,您必须在条件中创建数组,在您的代码中它用新值替换以前的值。

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

答案 1 :(得分:1)

您的错误在于使用public class MyAlarmService extends Service { private static final int NOTIFICATION_ID = 1; private NotificationManager notificationManager; private PendingIntent pendingIntent; private ArrayList<RandomQuotes> randomQuotesArrayList; private PrefManager prefManager; @Override public IBinder onBind(Intent arg0) { return null; } @SuppressWarnings("static-access") @Override public void onStart(Intent intent, int startId) { super.onStart(intent, startId); Context context = this.getApplicationContext(); prefManager = new PrefManager(this); randomQuotesArrayList = prefManager.getSharedPreferenceStringList(this, "quote"); notificationManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE); Intent mIntent = new Intent(this, MainActivity.class); pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_CANCEL_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this); builder.setVibrate(new long[]{5, 5, 5, 5, 5}); builder.setLights(Color.RED, 3, 3); builder.setContentTitle("Quote Of the Day"); builder.setContentText(randomQuotesArrayList.get(0).getRandomQuote() + "\n\n" + "-" + randomQuotesArrayList.get(0).getRandomQuoteAuthor()); String longText; if (!randomQuotesArrayList.get(0).getRandomQuoteAuthor().isEmpty()) { longText = randomQuotesArrayList.get(0).getRandomQuote() + "\n\n" + " -" + randomQuotesArrayList.get(0).getRandomQuoteAuthor(); } else { longText = randomQuotesArrayList.get(0).getRandomQuote() + "\n" +randomQuotesArrayList.get(0).getRandomQuoteAuthor(); } builder.setStyle(new NotificationCompat.BigTextStyle().bigText(longText)); builder.setSmallIcon(R.drawable.qt); builder.setContentIntent(pendingIntent); builder.setDefaults(Notification.DEFAULT_SOUND) .setDefaults(Notification.DEFAULT_ALL).setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); ; notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); notificationManager.notify(NOTIFICATION_ID, builder.build()); } } 附加到数组。请改用以下内容:

+=