0

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;

...
?>
2
  • You are overwriting the $mediagalleryURLs on each iteration of the foreach loop. Commented Nov 5, 2015 at 14:24
  • 1
    Should @mediagalleryURLs be an Array or a semicolon separated string? Commented Nov 5, 2015 at 14:29

3 Answers 3

1

You have to append the Urls:

foreach($item->images->image as $product_image) {
    $mediagalleryURLs .= rtrim($product_image) . ';';
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.';', ';');
}

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.