从php数组

时间:2017-10-16 09:45:56

标签: php arrays xml loops for-loop

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>

这是一个数组示例,您可以看到&#34; 425-DEEP_NAVY&#34;插入到数组中,大小为&#34; Medium&#34;但是后来又为每种尺寸一次又一次地插入:

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解决它。也就是说,产品被插入到数组中,用于每个&#34; productID&#34;有几个&#34;尺寸&#34;所以对于每个大小,相同的productID被插入几次,而我需要的是每个productID的大小是一个包含与该productID对应的所有大小的数组。请参阅下面的codepen控制台:

codepen example

1 个答案:

答案 0 :(得分:2)

组合数组

首先,您的问题可能在于您使用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_merge(),它将合并两个数组。

$array = ['test'];
$secondArray = ['foo', 'bar'];
$resultArray = array_merge($array, $secondArray);

这将为您提供$resultArray

Array(
    [1] => (string) "test"
    [2] => (string) "foo"
    [3] => (string) "bar"
)

转换为XML

您可以通过多种方式执行此操作。使用XmlWriter编写您自己的解决方案,或使用SimpleXmlDomDocument创建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

如果您想使用与示例中相同的格式/缩进,则需要使用DomDocumentXmlWriter。如果您已经在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);