PHPLover
PHPLover

Reputation: 12957

How to create a new array from the individual variables in PHP?

I've following variables' code which are to be inserted into an array. That is the new array should contain all these variables as its elements. How could I achieve this? My code for variables is as follows:

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

The new array should be called $form_data. Also pleas elet me know how to access the array elements the simplest and efficient way.

Upvotes: 0

Views: 92

Answers (6)

SagarPPanchal
SagarPPanchal

Reputation: 10111

<?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());

Upvotes: 0

Mario Radomanana
Mario Radomanana

Reputation: 1698

Use compact

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

Upvotes: 0

Nikhil Mohan
Nikhil Mohan

Reputation: 891

You can use compact function

https://www.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', '..');

Upvotes: 0

Rohit Awasthi
Rohit Awasthi

Reputation: 686

<?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();

Upvotes: 0

Satish Sharma
Satish Sharma

Reputation: 9625

$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;

Upvotes: 0

zzlalani
zzlalani

Reputation: 24344

Try this way

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

Upvotes: 6

Related Questions