从XML填充数组? PHP / XML

时间:2018-10-01 08:28:49

标签: php arrays 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("\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导出的结构?

谢谢!

1 个答案:

答案 0 :(得分:0)

通过使用对结构的了解并在需要时引用嵌套元素,而不是遍历所有子节点,可以使工作更加轻松。同样,使用$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>

缺少属性名称。