Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

160
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda