什么是生成跟随xlm的php数组设计?

时间:2015-10-01 20:05:18

标签: php arrays xml

我从这个link

获取了跟随的xml
<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
  <name>Belgian Waffles</name>
  <price>$5.95</price>
  <description>Our famous Belgian Waffles with plenty of real maple syrup</description>
  <calories>650</calories>
</food>
<food>
  <name>French Toast</name>
  <price>$4.50</price>
  <description>Thick slices made from our homemade sourdough bread</description>
  <calories>600</calories>
</food>
<food>
  <name>Homestyle Breakfast</name>
  <price>$6.95</price>
  <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
  <calories>950</calories>
</food>
</breakfast_menu>

我想知道可以生成上述xml的php数组。

如果我这样做:

$arrTest1=[
    "breakfast_menu"=>[

        "food"=>[
            "name"=>"Belgian Waffles",
            "price"=>"$5.95",
            "description"=>"Our famous Belgian Waffles with plenty of real maple syrup",
            "calories"=>650
        ],
        "food"=>[
            "name"=>"French Toast",
            "price"=>"$4.50",
            "description"=>"Thick slices made from our homemade sourdough bread",
            "calories"=>600
        ],
        "food"=>[
            "name"=>"Homestyle Breakfast",
            "price"=>"$6.95",
            "description"=>"Two eggs, bacon or sausage, toast, and our ever-popular hash browns",
            "calories"=>950
        ]

    ]
];

我将只有最后一个食物节点(相同的索引字符串)。

如果我这样做:

    $arrTest2=[
    "breakfast_menu"=>[        
     "food"=>[ 
                      [
                          "name"=>"Belgian Waffles",
                          "price"=>"$5.95",
                          "description"=>"Our famous Belgian Waffles with plenty of real maple syrup",
                          "calories"=>650
                      ],
                      [
                           "name"=>"French Toast",
                           "price"=>"$4.50",
                           "description"=>"Thick slices made from our homemade sourdough bread",
                           "calories"=>600
                      ],
                      [
                          "name"=>"Homestyle Breakfast",
                         "price"=>"$6.95",
                         "description"=>"Two eggs, bacon or sausage, toast, and our ever-popular hash browns",
                         "calories"=>950
                      ]
                ]
     ]
  ];

我的xml文档中没有使用SimpleXMLElement<food/>节点。相反,我将拥有节点<element0/><element1/>,.... 那么什么应该是最好的阵列设计可以生成回来的xml?

1 个答案:

答案 0 :(得分:0)

显然不能使用第一个阵列,因为&#34; food&#34;多次被替换,所以答案是使用第二个数组。

要使用SimpleXMLElement将其转换为XML,我认为您需要检查数组索引是否为数字,然后使用&#39; parent&#39; XML元素的数组键。

// modification from http://stackoverflow.com/questions/1397036
function array_to_xml( $data, &$xml_data, $node=NULL ) {
    foreach( $data as $key => $value ) {
        if( is_array($value) ) {
            if( is_numeric($key) ){
                $subnode = $xml_data->addChild($node); // use value in $node rather than $key to get parent array key instead of current numeric key
                array_to_xml($value, $subnode, $key);
            } else {
                array_to_xml($value, $xml_data, $key);
            }
        } else {
            $xml_data->addChild($key,htmlspecialchars($value));
        }
    }
}

$root = '<breakfast_menu />';
$x = new SimpleXMLElement($root);
array_to_xml($arrTest2['breakfast_menu'],$x);
header('Content-Type: text/xml');
echo $x->asXML();

此代码会将您的第二个数组转换为所需的XML。但是,如果数组包含更复杂的数组嵌套和索引键,它可能无法正常工作。