【问题标题】:Filling array from xml? PHP/XML从xml填充数组? PHP/XML
【发布时间】: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 导出的结构吗?

谢谢!

【问题讨论】:

    标签: php arrays xml


    【解决方案1】:

    您可以通过使用您对结构的了解并在需要时引用嵌套元素而不是遍历所有子节点来简化工作。同样使用$xmlstring-&gt;article 更明确地表明您希望该级别下的&lt;article&gt; 元素而不是任何其他数据。

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

    $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>
    

    缺少属性名称。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-22
      • 2017-06-05
      • 2019-08-20
      • 1970-01-01
      相关资源
      最近更新 更多