Reputation: 155
I have a foreach loop ($product_image) and an array ($newProductData), what I am trying to do is put the foreach content ($mediagalleryURLs) into the $newProductData array, when I print_r $newProductData, the the output shows only the last foreach element as $newProductData['media_gallery'] value, not the full elements, here is the code:
<?php
...
$resimURLNew = (string) $item->xpath('images/image[@main=1]')[0];
foreach($item->images->image as $product_image) {
$mediagalleryURLs = $product_image.';';
$mediagalleryURLs = rtrim($mediagalleryURLs, ';');
}
$newProductData = array(
'sku' => (string) $item->id,
'barcode' => (string) $item->barcode
);
$newProductData['image'] = (string) $resimURLNew;
$newProductData['small_image'] = (string) $resimURLNew;
$newProductData['thumbnail'] = (string) $resimURLNew;
$newProductData['media_gallery'] = $mediagalleryURLs;
...
?>
Upvotes: 0
Views: 62
Reputation: 544
Try this,
$newProductData = array(
'sku' => (string) $item->id,
'barcode' => (string) $item->barcode
);
foreach($item->images->image as $product_image) {
$mediagalleryURLs = $product_image.';';
$mediagalleryURLs = rtrim($mediagalleryURLs, ';');
$newProductData['media_gallery'] = $mediagalleryURLs;
}
$newProductData['image'] = (string) $resimURLNew;
$newProductData['small_image'] = (string) $resimURLNew;
$newProductData['thumbnail'] = (string) $resimURLNew;
Upvotes: 0
Reputation: 1057
You have to append the Urls:
foreach($item->images->image as $product_image) {
$mediagalleryURLs .= rtrim($product_image) . ';';
}
Upvotes: 1
Reputation: 45490
You are overwriting the variable when you should be appending elements to your array:
$mediagalleryURLs = array();
foreach($item->images->image as $product_image) {
$mediagalleryURLs[] = rtrim($product_image.';', ';');
}
Upvotes: 0