我有一个foreach循环($ product_image)和一个数组($ newProductData),我想要做的是将foreach内容($ mediagalleryURLs)放入$ newProductData数组,当我print_r $ newProductData时,输出仅将最后一个foreach元素显示为$ newProductData [' media_gallery']值,而不是完整元素,这里是代码:
<?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;
...
?>
答案 0 :(得分:1)
你必须追加网址:
foreach($item->images->image as $product_image) {
$mediagalleryURLs .= rtrim($product_image) . ';';
}
答案 1 :(得分:0)
当您要将元素附加到数组时,您将覆盖变量:
$mediagalleryURLs = array();
foreach($item->images->image as $product_image) {
$mediagalleryURLs[] = rtrim($product_image.';', ';');
}
答案 2 :(得分:0)
试试这个,
$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;