我有一堆要导出到 XML 文件的 SESSION 变量。客户详细信息很好,因为它们不是 foreach 循环的一部分。但是,我客户的订单是 foreach 循环的一部分。
SESSION 变量 $_SESSION['invoice'] 在 cart.php 中定义。$_SESSION['invoice'] 数组中的每个单独的值都需要检索并放入 XML。
例如
<title1>comic title1</title1><qty1>10</qty1><price1>$2.50</price1><cost1>$25.00</cost1><title2>comic title2</title2><qty2>5</qty2><price2>$2.00</price2><cost2>$10.00</cost2>
购物车.php 代码:
$_SESSION['invoice'][$comic_id]=$name . " " . $qty . " $" . $price . " $" . $cost;
xml.php 代码:
<?php
if(!isset($_SESSION)) {
session_start();
}
foreach ($_SESSION['invoice'] as $value);
$test_array = array (
$_SESSION['firstname'] => 'firstname',
$_SESSION['lastname'] => 'lastname',
$_SESSION['email'] => 'email',
$_SESSION['addressline1'] => 'addressline1',
$_SESSION['towncity'] => 'towncity',
$_SESSION['postcode'] => 'postcode',
'order' => array (
$_SESSION['total'] => 'total',
$_SESSION['invoice'] => 'order',
),
);
$xml = new SimpleXMLElement('<customer/>');
array_walk_recursive($test_array, array ($xml, 'addChild'));
print $xml->asXML();