PHP SESSION数组变为XML

时间:2012-11-12 08:43:22

标签: php xml session

我有一堆SESSION变量,我试图导出到XML文件。客户的详细信息很好,因为它们不是foreach循环的一部分。但是,我的客户订单是foreach循环的一部分。

SESSION变量$ _SESSION ['invoice']在cart.php中定义。需要检索$ _SESSION ['invoice']数组中的每个单独值并将其放入XML中。

e.g。

<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代码:

$_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();

2 个答案:

答案 0 :(得分:0)

    $_SESSION['invoice'][$comic_id]=$name . " " . $qty . " $" . $price . " $" . $cost;

使用此代码可以连接字符串。尝试调试创建的值,如:

    echo '<pre>'.print_r($_SESSION['invoice'][$comic_id], 1).'</pre>';
    echo '<pre>'.print_r($_SESSION['invoice'], 1).'</pre>';

您必须创建以下内容:

    $_SESSION['invoice'][$comic_id]['firstname'] = 'Firstname';
    $_SESSION['invoice'][$comic_id]['price'] = $myPrice';
    // and so on...

但有一点需要注意。不建议恕我直言,这样的发票处理会议!

答案 1 :(得分:0)

$ order = json_encode($ _ SESSION ['invoice']);