Creating variables in a array

I want to create this:

$main                 =    '';
$contactpersoon     =    '';
$email                =    '';
$telefoon            =    '';
$geboortedatum        =    '';
$scoutingnummer        =    '';
$scoutinggroep        =    '';
$noodtelefoon        =    '';
$contactpersoon2     =    '';
$email2                =    '';
$telefoon2            =    '';
$geboortedatum2        =    '';
$scoutingnummer2    =    '';
$scoutinggroep2        =    '';
$noodtelefoon2        =    '';
$contactpersoon3     =    '';
$email3                =    '';
$telefoon3            =    '';
$geboortedatum3        =    '';
$scoutingnummer3    =    '';
$scoutinggroep3        =    '';
$noodtelefoon3        =    '';

I thought it would work with this, but it doesn’t work, is there another way or is it just not possible?

$aNumber[0]='';
$aNumber[2]='2';
$aNumber[3]='3';


foreach ( $aNumber as $number)
        {
        
$contactpersoon.$number     =    '';
$email.$number                =    '';
$telefoon.$number            =    '';
$geboortedatum.$number        =    '';
$scoutingnummer.$number        =    '';
$scoutinggroep.$number        =    '';
$noodtelefoon.$number        =    '';
}

Well my first question is… why do you want to create that big long list of variables?

Assuming you dont want to do this with classes (a contactperson class), a standardized array would work…

$contacts = array();
for($i = 0; $i < 3; $i++) {
  $contacts[] = array('person' => '', 'email' => '', 'telefoon' => '', 'geboortdatum' => '', 'scoutingnummer' => '', 'scoutinggroep' => '', 'noodtelefoon' => '');
}

You then have a $contacts array that can be accessed.

$contacts[0][‘email’], etc.

1 Like

Thank you, it is part of a big form.

And I have to tell the script that the variables are empty

link to script:

http://www.jcsl.nl/scouting/index.php?pagina=inschrijven&activiteit=houthakkersweekend

Sorry but I’m pretty new with this.
But is there a way to check the output? If I echo it, the result is empty.

will this $contacts[0][‘email’], be the same as $email, $email2 and $email3?

so what you’ll get is an array (collection) of arrays (called a multi-dimensional array. but thats neither here nor there).

$contacts[0][‘email’] says: In the array of contacts, choose the 0th (numerical arrays constructed in this manner are numbered from 0, so 0 = first) element [which is an array], and give my the element of that array which corresponds to the key name ‘email’.

So to get email2, you’d want $contacts[1][‘email’], etc.

If you want to see what it looks like; after the for is closed, put this line:

print_r($contacts);

This will output the array to the screen (you might want to View Source, otherwise it will look ugly.), so it will make a bit more sense to you.

1 Like