【发布时间】:2019-03-06 07:59:49
【问题描述】:
我尝试从 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("\n\n### 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 导出的结构吗?
谢谢!
【问题讨论】: