douju9847 2016-06-02 13:13
浏览 52
已采纳

PHP:重新组合XML属性

I'm tried to parse XML response with DOM.

<GetBilletResult>
    <sNomProduit>product 2</sNomProduit>
    <sNomArticle>article 4</sNomArticle>
    ...
    <tabGrilleHoraire>
        <tabDetailTarifArticle>
            <tabDetail>
              <sDetail>Liste Pax : Pax n°1 [Âge:19]</sDetail>
              <sAgePax>19;</sAgePax>
              ...
            </tabDetail>
        </tabDetailTarifArticle>
        ...
    <tabGrilleHoraire>
</GetBilletResult>

I need to reorder the result and regroup the articles by products. The output should look like this :

  • sNomProduit : product 2

  • sNomArticle : article 1

  • tabGrilleHoraire

    • tabDetailTarifArticle
      • tabDetail
        • sDetail : Liste Pax : Pax n°1 [Âge:19]
        • sAgePax : 19;

Working code (thanks to ConstantineUA) :

$processed = array();
foreach( $billets as $GetBilletResult )
{ 
$sNomProduit = $GetBilletResult->getElementsByTagName( "sNomProduit" )->item(0)->nodeValue;
$nIDProduit = $GetBilletResult->getElementsByTagName( "nIDProduit" )->item(0)->nodeValue;
$sNomArticle = $GetBilletResult->getElementsByTagName( "sNomArticle" )->item(0)->nodeValue;
$nIDArticle = $GetBilletResult->getElementsByTagName( "nIDArticle" )->item(0)->nodeValue;
$tabDetail = $GetBilletResult->getElementsByTagName( "tabDetail" );

if (!isset($processed[$sNomProduit])) {
     $processed[$sNomProduit] = array();
}

$processed[$sNomProduit][] = array(
     'nIDProduit' => $nIDProduit,
     'sNomArticle' => $sNomArticle,
     'nIDArticle' => $nIDArticle,
     'tabDetail' => $tabDetail,
);
}

Loop:

foreach ($processed as $sNomProduit => $list) {
    echo "<h3> ".$sNomProduit."</h3>";
    foreach ($list as $item) {
        echo "<h5> ".$item['sNomArticle'] . "</h5>";
        foreach ($item['tabDetail'] as $node) {
            var_dump ($node->nodeValue);
        }
    }
    echo "<hr>";
}
  • 写回答

1条回答 默认 最新

  • duanhun3273 2016-06-02 13:45
    关注

    I think you can get by with an additional associative array where you put all you nodes during the loop:

    $processed = array();
    foreach( $billets as $GetBilletResult )
    { 
        $sNomProduit = $GetBilletResult->getElementsByTagName( "sNomProduit" )->item(0)->nodeValue;
        $nIDProduit = $GetBilletResult->getElementsByTagName( "nIDProduit" )->item(0)->nodeValue;
        $sNomArticle = $GetBilletResult->getElementsByTagName( "sNomArticle" )->item(0)->nodeValue;
        $nIDArticle = $GetBilletResult->getElementsByTagName( "nIDArticle" )->item(0)->nodeValue;
    
        if (!isset($processed[$sNomProduit])) {
             $processed[$sNomProduit] = array();
        }
    
        $processed[$sNomProduit][] = array(
             'nIDProduit' => $nIDProduit,
             'sNomArticle' => $sNomArticle,
             'nIDArticle' => $nIDArticle,
        );
    }
    

    And then loop through this array to display results:

    foreach ($processed as $sNomProduit => $list) {
        echo "<b>sNomProduit : </b> ".$sNomProduit."<br>";
        foreach ($list as $item) {
            echo "<b>sNomArticle : </b> ".$item['sNomArticle'] . "<br>";
            echo "<b>ListeTranche </b> ID_Tranche ".$item['nIDArticle'] . "<br>";
        }
    }
    

    It looks that your xml isn't complete so please note that it's more like a pseudo-code rather than a complete solution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题