从 xml 填充数组?PHP/XML



我尝试从xml文件中填充特定的数组。

这是我的xml结构,有两篇文章:

<?xml version="1.0" encoding="UTF-8"?>
<web-productexport>
<article key="5817203" status="active">
<productattributes>
<not_visible>
<feature key="product-type">
<xx description="Product-Type">Fix cable</xx>
</feature>
<feature key="headline_datasheet">
<xx description="Headline Datasheet">Fix cable, M12, S400</xx>
</feature>
<feature key="model">
<xx description="Model">axial</xx>
</feature>
</not_visible>
<group1>
<feature key="article_description">
<xx description="Article Description">BDX991-S400</xx>
</feature>
<feature key="_name">
<xx description="Name">5817203</xx>
</feature>
</group1>
</productattributes>
</article>
<article key="5817204" status="active">
<productattributes>
<not_visible>
<feature key="product-type">
<xx description="Product-Type">Fix cable</xx>
</feature>
<feature key="headline_datasheet">
<xx description="Headline Datasheet">Fix cable, M12, S340</xx>
</feature>
<feature key="model">
<xx description="Model">axial</xx>
</feature>
</not_visible>
<group1>
<feature key="article_description">
<xx description="Article Description">DDX991-S340</xx>
</feature>
<feature key="_name">
<xx description="Name">5817204</xx>
</feature>
</group1>
</productattributes>
</article>
</web-productexport>

由此,我需要以下数组结构中文章中的每个值:

$article = array(
'name' => 'BDX991-S400',
'active' => true,
'tax' => 19,
'supplier' => 'Conrad',    
),
'mainDetail' => array(
'number' => '5817203',
'active' => true
)
),
);

所以我需要像这样填写它: 名称 = 文章描述的值 (BDX991-S400(,编号 = 名称的值 (5817203(

这是我的 php 文件:

<?php
$servername = "localhost";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("nn### ERROR! ###" . $conn->connect_error);}
$xmlstring = simplexml_load_file("FILEPATH");
foreach($xmlstring->children() as $article) {
echo $articleid = (string) $article['key'];
foreach($article->children() as $productattr) {
foreach($productattr->children() as $visible)
foreach($visible->children() as $group) {
foreach($group->children() as $xx) {
$values=$xx;
echo $values;
}
}
}
}

我将 xx 的值放入字符串">$values"中,但我不知道如何将这些值放入上面提到的特定数组中。

有人可以帮助我吗?我被卡住了。。

还是应该更改 xml 导出的结构?

谢谢!

通过使用您对结构的了解并根据需要引用嵌套元素,而不是遍历所有子节点,可以使工作更轻松。 此外,使用$xmlstring->article更明确地表示您希望<article>元素位于该级别下,而不是任何其他数据。

这只是添加变量数据,您需要将相关的其他位添加到输出中......

$xmlstring = simplexml_load_file("data.xml");
$output = [];
foreach($xmlstring->article as $article) {
$articleData = [];
foreach ( $article->productattributes->group1->feature as $feature )   {
if ( $feature['key'] == "article_description" ) {
$articleData['name'] = (string)$feature->xx;
}
else if ( $feature['key'] == "_name" ) {
$articleData['mainDetail']['number'] = (string)$feature->xx;
}
}
$output[] = $articleData;
}
print_r($output);

这输出...

Array
(
[0] => Array
(
[name] => BDX991-S400
[mainDetail] => Array
(
[number] => 5817203
)
)
[1] => Array
(
[name] => DDX991-S340
[mainDetail] => Array
(
[number] => 5817204
)
)
)

另请注意,我必须像您一样修复几个属性。

<xx ="Product-Type">Fix cable</xx>

缺少属性的名称。

最新更新