Welcome Guest, Not a member yet? Register   Sign In
php associative arrays
#1

[eluser]caperquy[/eluser]
Hello,
I would like to know if it is possible to build an associative array and define the keys by the way of a variable.

In other words I would like to create an array such as the following

$table['lastname']="Durand";
$table['firstname']="Jean";

and use the content of a variable in order to define the keys lastname, firstname and so on.

Many thanks to whoever can give me a solution if it exists.

CapErquy
#2

[eluser]Boris Strahija[/eluser]
Can you maybe explain the problem a little bit better, I'm don't quite understand what it is you're trying to do.
#3

[eluser]caperquy[/eluser]
I am building a dropdown list using form_dropdown. I pass an associative array of options which results in something like :

<option value="0">Arménie</option>
<option value="1">France</option>
<option value="2">Brésil</option>

My problem is that the submit returns, for example 1 to my controller, but I then do not know how to identify the string France which is the only thing I am interested in.
#4

[eluser]Boris Strahija[/eluser]
Well where do you have the country list stored? Is it a database table, or a config file?
If it's a db table then just add an ID to every country, or maybe a country short code, and put this ID or short code as the value into the dropdown.

You can also do it with an array in a config file. Something like this:
Code:
$config['countries']['ar'] = 'Arménie';
$config['countries']['fr'] = 'France';
$config['countries']['br'] = 'Brésil';

That way it should be quite easy to get what you want.
#5

[eluser]CroNiX[/eluser]
[quote author="caperquy" date="1294023268"]I am building a dropdown list using form_dropdown. I pass an associative array of options which results in something like :

<option value="0">Arménie</option>
<option value="1">France</option>
<option value="2">Brésil</option>

My problem is that the submit returns, for example 1 to my controller, but I then do not know how to identify the string France which is the only thing I am interested in.[/quote]
If you aren't going to use or need the actual key (the country is all you are interested in), then why not put IT as the key?
<option value=“Arménie”>Arménie</option>
<option value=“France”>France</option>
<option value=“Brésil”>Brésil</option>
#6

[eluser]caperquy[/eluser]
This is exactly what I would like to do but since the list of countries is not fixed and changes from time to time I do not see how to create an associative array where the key is built from a variable which contains the country name. All php examples that I find in the manuals show something like :
$table = array{
"country1" => "Armenie",
"country2" => "France"
.....
}

I must be missing something, but what ?
#7

[eluser]Boris Strahija[/eluser]
So you're saying you have a simple array (list) of countries, something like this:
Code:
$countries = array('Armenia', 'France', 'Brasil', 'USA', 'Croatia');

And the list changes from time to time, and you can't chnage the way this list is built?
If that's the case, you could do something like this:
Code:
$dropdow_countries = array();
foreach ($countries as $country) {
    $dropdow_countries[$country] = $country;
}
#8

[eluser]caperquy[/eluser]
Many thanks for help. I will try that.




Theme © iAndrew 2016 - Forum software by © MyBB