dql123000 2015-03-11 03:09
浏览 58
已采纳

如何在PHP中将XML数据的元素转换为单个关联数组?

I have a piece of PHP code which returns an object variable with XML structure as follows:

<food1>
    <name1>Belgian Waffles</name1>
    <prices1>
        <price1>$5.95</price1>
        <price2>$8.95</price2>
    </prices1>
    <description1>Two of our famous Belgian Waffles with plenty of real maple syrup</description1>
    <calories1>
            <AA>
                <A1>650</A1>
                <A2>652</A2>
                <A3>653</A3>
            </AA>
            <BB>
                <B1>750</B1>
                <B2>751</B2>
                <B3>752</B3>
            </BB>
    </calories1>
</food1>

How to write a foreach loop in PHP that produces a variable like $food1 from the above? Then my plan is to insert the $food1 value into a text field in a MySQL database to parse and process the $food1 array whenever is needed.

   $food1 = array( 
        "food1" => array
        (
        "name1" => Belgian Waffles,        
        "prices1" => array (
                            price1 => $5.95
                            price2 => $8.95
                            ),      
        "description1" => 'Two of our famous Belgian Waffles with plenty of real maple syrup',
        "calories1" => array
                (
                "AA" => array (
                              A1 => 650
                              A2 => 652
                              A3 => 653
                               ),
                "BB" => array (
                              B1 => 750
                              B2 => 752
                              B3 => 753
                               ),
                )
         );
  • 写回答

1条回答 默认 最新

  • doumianfeng6979 2015-03-11 03:36
    关注

    One remark in your XML change this:

    <name2>Belgian Waffles</name1>
    

    to

    <name1>Belgian Waffles</name1>
    

    Try this code:

    <?php
    
    $xml_string = '<food1>
        <name1>Belgian Waffles</name1>
        <prices1>
            <price1>$5.95</price1>
            <price2>$8.95</price2>
        </prices1>
        <description1>Two of our famous Belgian Waffles with plenty of real maple syrup</description1>
        <calories1>
                <AA>
                    <A1>650</A1>
                    <A2>652</A2>
                    <A3>653</A3>
                </AA>
                <BB>
                    <B1>750</B1>
                    <B2>751</B2>
                    <B3>752</B3>
                </BB>
        </calories1>
    </food1>';
    
    $xml = simplexml_load_string($xml_string);
    $json = json_encode($xml);
    
    $out = array($xml->getName() => json_decode($json, true));
    
    print_r($out);
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,