我有一堆SESSION变量,我试图导出到一个XML文件。客户细节很好,因为它们不是foreach循环的一部分。但是,我的客户订单是foreach循环的一部分。PHP SESSION数组变量为XML
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>
cart.php代码:
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();
这种方式不会工作,因为所有连接的值/行不会从数组中拉回来。 – Accolade
$ order = json_encode($ _ SESSION ['invoice']); – Accolade