如何从PHP中的各个变量创建一个新数组?

时间:2014-02-28 06:36:49

标签: php arrays variables

我遵循要插入数组的变量代码。也就是说,新数组应该包含所有这些变量作为其元素。我怎么能实现这个目标?我的变量代码如下:

  $question_id        = $request['question_id'];
  $reported_site_id   = SITE_ID;
  $reported_user_type = $_SESSION[SESSION_NAME_CONTROL][STAFF_TYPE];
  $reported_user_id   = $_SESSION[SESSION_NAME_CONTROL][STAFF_ID];
  $que_issue          = implode(",", $request['que_issue']);
  $que_issue_comment  = $request['que_issue_comment']; 
  $que_issue_date     = time();

应该将新数组称为$form_data。同时请求我知道如何以最简单有效的方式访问数组元素。

6 个答案:

答案 0 :(得分:6)

试试这种方式

$form_data = array(
  'question_id'        => $request['question_id'],
  'reported_site_id'   => SITE_ID,
  'reported_user_type' => $_SESSION[SESSION_NAME_CONTROL][STAFF_TYPE],
  'reported_user_id'   => $_SESSION[SESSION_NAME_CONTROL][STAFF_ID],
  'que_issue'          => implode(",", $request['que_issue']),
  'que_issue_comment'  => $request['que_issue_comment'],
  'que_issue_date'     => time()
);

答案 1 :(得分:0)

$question_id        = $request['question_id'];
$reported_site_id   = SITE_ID;
$reported_user_type = $_SESSION[SESSION_NAME_CONTROL][STAFF_TYPE];
$reported_user_id   = $_SESSION[SESSION_NAME_CONTROL][STAFF_ID];
$que_issue          = implode(",", $request['que_issue']);
$que_issue_comment  = $request['que_issue_comment']; 
$que_issue_date     = time();

$form_data = array();

$form_data['question_id'] = $question_id;
$form_data['reported_site_id'] = $reported_site_id;
$form_data['reported_user_type'] = $reported_user_type;
$form_data['reported_user_id'] = $reported_user_id;
$form_data['que_issue'] = $que_issue;
$form_data['que_issue_comment'] = $que_issue_comment;
$form_data['que_issue_date'] = $que_issue_date;

答案 2 :(得分:0)

<?php
$form_data = array();
$form_data['question_id'] = $question_id    = $request['question_id'];
$form_data['reported_site_id'] =   $reported_site_id   = SITE_ID;
$form_data['reported_user_type'] =  $reported_user_type = $_SESSION[SESSION_NAME_CONTROL][STAFF_TYPE];
$form_data['reported_user_id'] =   $reported_user_id   = $_SESSION[SESSION_NAME_CONTROL][STAFF_ID];
$form_data['que_issue'] =   $que_issue    = implode(",", $request['que_issue']);
$form_data['que_issue_comment'] =   $que_issue_comment  = $request['que_issue_comment']; 
$form_data['que_issue_date'] =   $que_issue_date     = time();

答案 3 :(得分:0)

您可以使用compact功能

http://in1.php.net/compact

  $question_id        = $request['question_id'];
  $reported_site_id   = SITE_ID;
  $reported_user_type = $_SESSION[SESSION_NAME_CONTROL][STAFF_TYPE];
  $reported_user_id   = $_SESSION[SESSION_NAME_CONTROL][STAFF_ID];
  $que_issue          = implode(",", $request['que_issue']);
  $que_issue_comment  = $request['que_issue_comment']; 
  $que_issue_date     = time();

  $new_array = compact('question_id', 'reported_site_id', '..');

答案 4 :(得分:0)

使用compact

$form_date = compact('question_id', 'reported_site_id', 'reported_user_type', 'reported_user_id', 'que_issue', 'que_issue_comment', 'que_issue_date');

答案 5 :(得分:0)

<?php
    $form_data = array();
    $form_data[] = ('question_id'=>$request['question_id']);
    $form_data[] = ('reported_site_id'=>SITE_ID);
    $form_data[] = ('reported_user_type'=>$_SESSION[SESSION_NAME_CONTROL][STAFF_TYPE]);
    $form_data[] = ('reported_user_id'=>$_SESSION[SESSION_NAME_CONTROL][STAFF_ID]);
    $form_data[] = ('que_issue'==>implode(",", $request['que_issue']));
    $form_data[] = ('que_issue_comment'=>$request['que_issue_comment']); 
    $form_data[] = ('que_issue_date'=>time());