【发布时间】:2012-02-03 05:03:22
【问题描述】:
我是 stackoverflow 的新手,所以请提前原谅我。 :)
我正在尝试合并具有相同 BranchCode 的对象,基本上,只是将 Branch 作为主 Product 节点的子节点。请在下面查看我的示例 XML。谢谢。
我有这个 XML (simpleXMLElement->asXML()):
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>14</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>150</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>226</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>227</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>26</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>34</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>35</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>400A</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>405A</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>460A</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>57</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>83</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>C3</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-02-2166</ProductCode>
<BranchCode>Global</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>14</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>150</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>226</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>227</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>26</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>34</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>35</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>400A</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>405A</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>460A</BranchCode>
<Available>5.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>57</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>83</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>C3</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>0.00</AvailableGlobally>
</Product>
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-051-2030</ProductCode>
<BranchCode>Global</BranchCode>
<Available>0.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>5.00</AvailableGlobally>
</Product>
</ArrayOfProduct>
我想要一个类似这样的输出:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Product>
<Customer>238500</Customer>
<ProductCode>AAA-50-3535</ProductCode>
<Branch>
<BranchCode>C3</BranchCode>
<Available>10.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>100.00</AvailableGlobally>
</Branch>
<Branch>
<BranchCode>A5</BranchCode>
<Available>20.00</Available>
<AvailableCSL>0.00</AvailableCSL>
<AvailableGlobally>100.00</AvailableGlobally>
</Branch>
....
....
</Product>
....
....
</ArrayOfProduct>
【问题讨论】:
-
该 XML 文档中还有其他内容吗?如果没有,最简单的方法是通过迭代每个产品并复制其数据来以正确的格式创建新文档。否则,如果您必须修改现有文档的结构,使用 DOM(甚至可能是 XSLT)比使用 SimpleXML 更简单。
-
@JoshDavis:是的,XML 中的其他数据看起来像具有不同 ProductCode 的数据。请查看更新后的帖子。感谢您花时间查看我的问题。干杯!
标签: php arrays multidimensional-array merge simplexml