Luigi Spezia Luigi Spezia - 2 months ago 5
PHP Question

Switch statement : get the same result but with less code

I have the impression that this script can be rewritten in a shorter way but I can not figure out how .
Is there a shorter way to do this ?

switch($n){
case 1:
$eta1 = $_POST['eta_1'];
break;
case 2:
$eta1 = $_POST['eta_1'];
$eta2 = $_POST['eta_2'];
break;
case 3:
$eta1 = $_POST['eta_1'];
$eta2 = $_POST['eta_2'];
$eta3 = $_POST['eta_3'];
break;
case 4:
$eta1 = $_POST['eta_1'];
$eta2 = $_POST['eta_2'];
$eta3 = $_POST['eta_3'];
$eta4 = $_POST['eta_4'];
break;
case 5:
$eta1 = $_POST['eta_1'];
$eta2 = $_POST['eta_2'];
$eta3 = $_POST['eta_3'];
$eta4 = $_POST['eta_4'];
$eta5 = $_POST['eta_5'];
break;
case 6:
$eta1 = $_POST['eta_1'];
$eta2 = $_POST['eta_2'];
$eta3 = $_POST['eta_3'];
$eta4 = $_POST['eta_4'];
$eta5 = $_POST['eta_5'];
$eta6 = $_POST['eta_6'];
break;
case 7:
$eta1 = $_POST['eta_1'];
$eta2 = $_POST['eta_2'];
$eta3 = $_POST['eta_3'];
$eta4 = $_POST['eta_4'];
$eta5 = $_POST['eta_5'];
$eta6 = $_POST['eta_6'];
$eta7 = $_POST['eta_7'];
break;
case 8:
$eta1 = $_POST['eta_1'];
$eta2 = $_POST['eta_2'];
$eta3 = $_POST['eta_3'];
$eta4 = $_POST['eta_4'];
$eta5 = $_POST['eta_5'];
$eta6 = $_POST['eta_6'];
$eta7 = $_POST['eta_7'];
$eta8 = $_POST['eta_8'];
break;
case 9:
$eta1 = $_POST['eta_1'];
$eta2 = $_POST['eta_2'];
$eta3 = $_POST['eta_3'];
$eta4 = $_POST['eta_4'];
$eta5 = $_POST['eta_5'];
$eta6 = $_POST['eta_6'];
$eta7 = $_POST['eta_7'];
$eta8 = $_POST['eta_8'];
$eta9 = $_POST['eta_9'];
break;
case 10:
$eta1 = $_POST['eta_1'];
$eta2 = $_POST['eta_2'];
$eta3 = $_POST['eta_3'];
$eta4 = $_POST['eta_4'];
$eta5 = $_POST['eta_5'];
$eta6 = $_POST['eta_6'];
$eta7 = $_POST['eta_7'];
$eta8 = $_POST['eta_8'];
$eta9 = $_POST['eta_9'];
$eta10 = $_POST['eta_10'];
break;
default:
break;
}


The name reflects the value of the variable $ _POST received .

Using the code posted by @Don't Panic I need to update and to insert the values ​​of variables inside an existing array.

for ($i=1; $i <= $n_bambini; $i++) {
${"eta$i"} = $_POST["eta_$i"];
$_SESSION['dati_cerca_vacanza'] = ["eta$i"=>${"eta$i"}];
}


The array already exists in this format :

$_SESSION['dati_cerca_vacanza']= ['citta'=>$citta, 'tipo_vacanza'=>$tipo_vacanza, 'lingua'=>$lingua, 'data_arrivo'=>$data_arrivo, 'data_partenza'=>$data_partenza, 'n_adulti'=>$n_adulti, 'n_bambini'=>$n_bambini];


Because it does not work?

Answer

One way with variable variables:

for ($i=1; $i <= $n; $i++) { 
    ${"eta$i"} = $_POST["eta_$i"];
}

The ${"eta$i"} creates your various $eta1, $eta2, etc. variable names and $_POST["eta_$i"] gets the corresponding values from $_POST.


Regarding your edit, if you want to append these values to that session key, instead of this

$_SESSION['dati_cerca_vacanza'] = ["eta$i"=>${"eta$i"}];

You can use this

$_SESSION['dati_cerca_vacanza']["eta$i"] = $_POST["eta_$i"];

The way you're doing it would replace the entire contents of $_SESSION['dati_cerca_vacanza'] with each iteration.