0

I have the follow xml that I took from this link

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

I would like to know the php array that could generate the above xml.

If I do this:

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

    ]
];

I will have only the last food node (same index string).

If I do this:

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

I will not have the <food/> node in my xml document, using SimpleXMLElement. Instead, I will have the node <element0/>, <element1/>, ... . So what should be the best array design that could generate back the xml?

Community
  • 1
  • 1
IgorAlves
  • 5,086
  • 10
  • 52
  • 83

1 Answers1

0

Obviously the first array can't be used, because the value of "food" gets replaced several times so the answer is use the second array.

To use SimpleXMLElement to transform it to XML, I think you need to check if the array indexing is numeric and then use the 'parent' array key for the XML element.

// 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();

This code will transform your second array into the desired XML. But, if the array contained a more complex mixture of array nesting and indexing keys, it might not work as desired.

Michael Curtis
  • 394
  • 1
  • 3
  • 14