在for循环中,我有以下代码,它推送一个数组,其中包含另一个数组中的一些数据:
$finCombo = array("color" => $color, "size" => $size, "productID" => $theProductID."-".$color, "imageURL" => 'https://click-tshirt.gr/images/detailed/all/'.$theProductCode."_".strtolower($color).'.jpg', "productURL" => $theProductURL, "title" => $theProductName, "price" => $theProductPrice, "category_path" => $theCategoryName, "availability" => $theProductAvailability, "brand" => $theProductBrand, "SKU" => $theProductCode."-".$color);
array_push($finalCmb, $finCombo);目标是使用数组中的数据创建XML文件。代码应该返回如下示例所示的xml文件:
<store>
<date>2017-09-22 19:30</date>
<products>
<product>
<SKU>10206-BLACK</SKU>
<productID>338-BLACK</productID>
<size>Small, Medium, Large, X Large</size>
<color>BLACK</color>
<title>Women's Jersey Short Sleeve Deep V-Neck Tee BLACK</title>
<productURL>https://click-tshirt.gr/el-4/t-shirt-el/womens-jersey-short-sleeve-deep-v-neck-tee/</productURL>
<imageURL>https://click-tshirt.gr/images/detailed/all/10206_Black.jpg</imageURL>
<price>9.90</price>
<category_path>ΓΥΝΑΙΚΕΙΑ///T-SHIRT</category_path>
<brand>BELLA&CANVAS</brand>
<availability>Κατόπιν παραγγελίας</availability>
</product>
这里是一个数组的示例,您可以看到在数组中插入了“425深海军”,其大小为"Medium“,但随后会对每个大小一次又一次地插入:
Array ( [0] => Array ( [0] => Array ( [color] => BLACK_DARK_GREY [size] => Small [productID] => 390-BLACK_DARK_GREY [imageURL] => https://click-tshirt.gr/images/detailed/all/18608_black_dark_grey.jpg [productURL] => http://click-tshirt.gr/el-3/category-50/adult-fashion-basic-ls-hooded-tee/ [title] => Adult Fashion Basic LS Hooded Tee [price] => 13.800000 [category_path] => ΑΝΔΡΙΚΑ/ΦΟΥΤΕΡΑΚΙΑ ΜΕ ΚΟΥΚΟΥΛΑ [availability] => Κατόπιν παραγγελίας [brand] => ANVIL [SKU] => 18608-BLACK_DARK_GREY )))还有一件事发生了,到目前为止,我已经设法用JavaScript解决了它。也就是说,产品被插入到数组中,对于每个" productID“,有几个”大小“,因此对于每个大小,相同的productID被插入了几次,而我需要的是,对于每个productID,大小是包含与该productID对应的所有大小的数组。请参见下面的codepen控制台:
发布于 2017-10-16 09:57:00
组合阵列
首先,您的问题可能在于您使用array_push。
$array = ['test'];
$secondarray = ['foo', 'bar'];
array_push($array, $secondarray);将为您提供一个包含数组$array的$secondarray。
Array (
0 => (string) "test"
1 => Array (
0 => (string) "foo"
1 => (string) "bar"
)
)我猜您需要的是合并(),它将合并两个数组。
$array = ['test'];
$secondArray = ['foo', 'bar'];
$resultArray = array_merge($array, $secondArray);这会给你$resultArray
Array(
[1] => (string) "test"
[2] => (string) "foo"
[3] => (string) "bar"
)转换为XML
你可以用多种方式来做这件事。使用XmlWriter编写自己的解决方案,或者使用SimpleXml或DomDocument创建DOM结构。
SimpleXml
也许最简单的解决方案是像这样使用SimpleXmlElement:
$xml = SimpleXmlElement('<store/>');
$xml->addChild('date', '2017-09-22 19:30');
$products = $xml->addChild('products');
$product = $products->addChild('product');
foreach ($finCombo as $name => $value) {
$product->addChild($name, $value);
}
echo $xml->asXML(), PHP_EOL;带有格式的SimpleXml
如果要使用与示例中相同的格式/缩进,则需要使用DomDocument或XmlWriter。如果您已经在SimpleXml中这样做了,您可以轻松地从一个SimpleXmlElement加载一个DomDocument。
...
$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
$domxml->dom_import_simplexml($xml); // load the SimpleXmlElement previously created.
echo $domxml->saveXML();DomDocument
或者你可以直接用DOMDocument来做这件事。
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$store = $dom->createElement('store');
$dom->appendChild($store);
$store->appendChild($dom->createElement('date', '2017-09-22 19:30'));
$products = $dom->createElement('products');
$product = $dom->createElement('product');
foreach ($finCombo as $key => $val) {
$product->appendChild($dom->createElement($key, $val));
}
$products->appendChild($product);
$store->appendChild($products);
echo $dom->saveXML(), PHP_EOL;XmlWriter
或者,如果您想要更多的控制(例如,对于缩进),可以使用XmlWriter
$writer => new XMLWriter();
$writer->openMemory();
$writer->setIndent(true);
$writer->setIndentString(' ');
$writer->startDocument('1.0', 'UTF-8');
$writer->startElement('store');
$writer->startElement('date');
$writer->text('2017-09-22 19:30');
$writer->fullEndElement(); // end date element
$writer->startElement('products');
$writer->startElement('product');
foreach ($finCombo as $key => $val) {
$writer->startElement($key);
$writer->text($val);
$writer->fullEndElement(); // end $key element
}
$writer->fullEndElement(); // end product element
$writer->fullEndElement(); // end products element
$writer->fullEndElement(); // end store element
$writer->endDocument();
echo $writer->flush(true);https://stackoverflow.com/questions/46767320
复制相似问题