PHP:为会话分配多维数组

时间:2012-12-19 11:22:18

标签: php arrays session

处理项目并遇到将多维数组分配给会话的挑战。

项目信息:

公司需要注册/预订网站上的课程,公司可以注册多个课程,每个课程,公司必须指定候选人的数量及其生物数据。

示例:

如果公司XYZ注册课程A,课程B和课程C.并且每门课程都提供以下候选信息:

<?php

 $all_reg_courses = array
  (
  $course_id => array(
    "candidate_no" => $cand_no,
  "all_candidates" => array(
 array
  (
  'cand_name' => 'Alex Holmes',
  'cand_email' => 'al@yah.com',
  'cand_sex' => 'Male',
  'cand_phone' => '070043635690',
  'cand_job' => 'IT'
  ),
 array
  (
  'cand_name' => 'Tilla Reel',
  'cand_email' => 'till@yah.com',
  'cand_sex' => 'Female',
  'cand_phone' => '0700833435690',
  'cand_job' => 'HR'
  ),
 array
  (
  'cand_name' => 'Bill Enya',
  'cand_email' => 'bill@yah.com',
  'cand_sex' => 'Male',
  'cand_phone' => '07008055435690',
  'cand_job' => 'Admin'
  )
  )
  ),
  $course_id => array(
    "candidate_no" => $cand_no,
  "all_candidates" => array(
 array
  (
  'cand_name' => 'Alan Pole',
  'cand_email' => 'al@yah.com',
  'cand_sex' => 'Male',
  'cand_phone' => '07009973635690',
  'cand_job' => 'IT'
  ),
 array
  (
  'cand_name' => 'Coleen Curtis',
  'cand_email' => 'cole@yah.com',
  'cand_sex' => 'Female',
  'cand_phone' => '5553435690',
  'cand_job' => 'HR'
  )
  )
  ),
    $course_id => array(
    "candidate_no" => $cand_no,
  "all_candidates" => array(
 array
  (
  'cand_name' => 'Tamia James',
  'cand_email' => 'tam@yah.com',
  'cand_sex' => 'Female',
  'cand_phone' => '7673435690',
  'cand_job' => 'HR'
  )
  )
  )
  );

?>

为各种课程提供的这些数据以及为每门课程提名的候选人将临时存储在会话中,如果“公司XYZ”最终点击“最终提交按钮”,则最终将被发送到数据库中。会议将在特定时间后销毁(即课程预订车将被清空)

我一直在尝试将上面的示例多维数组分配到会话中,但它不仅仅是在工作。

收到错误消息:无法使用字符串偏移量;

以下是将数组附加到会话的代码:

<?php

session_name("cand_cart");
session_start();

$c_id = $_POST['c_id'];
//$cand_no = $_POST['cand_no'];

$_SESSION['cand_cart']['c_id']['cands'] = array();
$_SESSION['cand_cart']['c_id'] = array();
$_SESSION['cand_cart']['c_id']['cand_no'] = array();

$_SESSION['cand_cart']['c_id']['cand_no'] = $_POST['cand_no'];
$_SESSION['cand_cart']['c_id'] = $_POST['c_id'];

//$candidates = array();

foreach ($_POST['candidate'] as $i => $can)
{
    $candidates[$i] = array();

    foreach ($can as $key => $value)
    {
        //$candidates[$i][$key] = $value;
        $_SESSION['cand_cart']['c_id']['cands'][$i][$key] = $value;
    }
}


echo '<pre>';
//var_dump($candidates);
print_r($_SESSION['cand_cart']);
echo '</pre>';

echo "<br><br>";

?>

<table border="1" cellspacing="0" cellpadding="6">
    <thead>
        <th>#</th>
        <th>Name</th>
        <th>Phone</th>
        <th>Email</th>
        <th>Sex</th>
        <th>Position</th>
    </thead>
    <tbody>
<?php

foreach ($_SESSION['cand_cart'] as $i => $can)
{
    $c = (object) $can;
    $sn = $i + 1;
    echo '<tr><td>' . $sn . '</td><td>' . $c->name . '</td><td>' . $c->phone . '</td><td>' . $c->email . '</td><td>' . $c->sex . '</td><td>' . $c->pos . '</td></tr>';
}

?>
    </tbody>
</table>

非常感谢获得这方面的帮助..谢谢!

3 个答案:

答案 0 :(得分:1)

尝试更改这些行:

$_SESSION['cand_cart']['c_id']['cands'] = array();
$_SESSION['cand_cart']['c_id'] = array();

$_SESSION['cand_cart']['c_id'] = array();
$_SESSION['cand_cart']['c_id']['cands'] = array();

答案 1 :(得分:0)

您正在覆盖整个阵列,丢失以前存储的信息

$_SESSION['cand_cart']['c_id']['cands'] = array();
$_SESSION['cand_cart']['c_id'] = array(); // here you overwrite the whole 'c_id' array and loose cands

$_SESSION['cand_cart']['c_id']['cand_no'] = $_POST['cand_no'];
$_SESSION['cand_cart']['c_id'] = $_POST['c_id']; // another overwite

答案 2 :(得分:-1)

在将数据存储到会话之前序列化和反序列化数据要容易得多。这似乎更轻量级,您不必具体设置每个值:

// Build up your data-Array from the submitted values stored in $_POST
$data = array();
foreach($_POST['candidate'] AS $i => $can)
{
     // Do your logic here, like you did before with the session
}

// Now serialize and store into session at once (= more performance)
$_SESSION['data'] = serialize($data);

如果要在下一个请求中使用数据,则必须对其进行反序列化:

$data = unserialize($_SESSION['data']);

希望这有帮助。