Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
arrays and foreach loops to work with input vars more efficiently

If I have 20 user input vars and want to work between $var[0..19], $_SESSION['var'[0..19]], & $_POST['var'[0..19]], such as:

$var0 = $_SESSION['var0'] = $_POST['var0'];

How could I do this efficiently using arrays and foreach loops?

Additional details:

So initially I have been thinking to try something like:

$inputVars = array('var1'=>$var1, 'var2'=>$var2, 'var3'=>$var3, 'var4'=>$var4, 'var5'=>$var5)

$inputVarsS = array($_SESSION['inputVars = array('svar1'=>$_SESSION['var1']'], 'svar2'=>$_SESSION['var2'], 'svar3'=>$_SESSION['var3'], 'svar4'=>$_SESSION['var4'], 'svar5'=>$_SESSION['var5'])

$inputVarsP = array($_POST['inputVars = array('pvar1'=>$_POST['pvar1']'], 'pvar2'=>$_POST['pvar2'], 'pvar3'=>$_POST['pvar3'], 'pvar4'=>$_POST['pvar4'], 'pvar5'=>$_POST['pvar5'])

and then I guess I could do something like:

$inputVarsS['svar1'] = $inputVarsP['pvar1'];

to set session value svar1 to posted value pvar1. But I am thinking there really must be a more efficient way to do these kinds of assignments.

I think I could just easily have $inputVars array and assign either POST SESSION values to the keys, but what if I want to have POST and SESSION values available simultaneously? Then I assume I need multiple arrays?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can use $$ to define a variable name, but I think it's better to use an array $vars.

With $var0, $var1, ...., and you can access them like echo $var4

foreach(range(0, 19) as $index)
{
  $var = 'var' . $index;
  $$var = $_SESSION[$var] = $_POST[$var];
}

With array $vars, and access them with echo $vars[4]

foreach(range(0, 19) as $index)
{
  $var = 'var' . $index;
  $vars[$var] = $_SESSION[$var] = $_POST[$var];
}
about 4 years ago · Santiago Trujillo Report

0

Your question lacks information, so my answer can be wrong.

You can copy an whole array like this:

$_SESSION['post'] = $_POST;

Then when you want to extract variables from that, you can use:

extract($_SESSION['post']);

So if $_POST['var0'] exists it will extract $var0.

However, you should NEVER put posted data in global scope. Hackers can use this to inject their variables into your code. Please always filter input first. A bit better is this:

extract($_SESSION['post'],EXTR_PREFIX_ALL,'post');

So it will be $post_var0 instead of $var0. But still, make sure you only get the variables you want, and filter their values.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!