Problem with hyphens in array keys

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Problem with hyphens in array keys

Post by mecha_godzilla »

Hi,

Before I start outlining the problem, I know that hyphens in variable names are bad juju :lol:

I'm using a JavaScript date picker to populate some drop-down menus. In the form, the code for the "which day of the month" drop-down menu looks like this:

Code: Select all

<select id="date-sel-dd" name="date-sel-dd">
...
</select>
What I do in my PHP script is have an array of values, the keys for which have to match the IDs of the form elements. Because I have to parse some of these variables after they've been received from the form, I then do this in my script:

Code: Select all

foreach($this->form_array as $key => $value) {
    $val = $key;
    $$val = $value['value'];
}
Needless to say, the array keys with hyphens in them don't get assigned properly.

Does anyone know of a way to escape the array key so that the dynamically-created variable is assigned correctly? The date picker script is going to be a bit of a nightmare to edit so I'd like a quick win on this one if possible.

Thanks in advance - and a big shout out to all those developers working hard this weekend, keeping the economy ticking over for everyone and setting a good example generally! :mrgreen:

Mecha Godzilla
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with hyphens in array keys

Post by requinix »

Don't use variable variables. There is maybe one situation in one hundred where it's appropriate and this isn't it.

Access the array like a normal array: $this->form_array["date-sel-dd"].
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Problem with hyphens in array keys

Post by mecha_godzilla »

Hi,

I have to use the variables quite a lot in the script, so things could get a bit messy - my script will end up looking like this:

Code: Select all

if ($this->form_array['date-sel-dd']['value'] > $last_day_of_the_month) {
    //
}
rather than (in theory) this:

Code: Select all

if ($date-sel-dd > $last_day_of_the_month) {
    //
}
Also, with the first approach I'm hard-coding the format of my form array everywhere in my scripts, whereas I don't currently have to do this.

Thanks,

M_G
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with hyphens in array keys

Post by requinix »

mecha_godzilla wrote:I have to use the variables quite a lot in the script, so things could get a bit messy - my script will end up looking like this:
If you want shorter variable names then you can just make a copy.

Code: Select all

$date_sel_dd = $this->form_array['date-sel-dd']['value'];
mecha_godzilla wrote:Also, with the first approach I'm hard-coding the format of my form array everywhere in my scripts, whereas I don't currently have to do this.
And you're worried that you might restructure everything in the future?
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Problem with hyphens in array keys

Post by mecha_godzilla »

requinix wrote:If you want shorter variable names then you can just make a copy.
Ok, that sounds like a sensible plan - thank you :D
requinix wrote:And you're worried that you might restructure everything in the future?
That's extremely likely - the code I've got works at the moment while the number of form elements is still manageable, but I might eventually have to move everything over to a different system once I've fully realised the shortcomings of my current approach. The forms have to support a wide range of different access levels with certain (mandatory) fields hidden depending on who the user is, so I'm kind of making it up as I go along at the moment...

Thanks,

M_G
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with hyphens in array keys

Post by requinix »

If you end up restructuring everything then no matter what you do now you'll have to change it.
If you automatically create variables like you were originally planning then those variable names might change and you'd have to go through all your code to change that. If you access form_array like I suggested then you'll have to go through all your code to change that too. At least with a copy all you'd do is change one line of code (although the variable name might not match up with the form field anymore).

Try some Test Driven Development for this. Come up with something that you need your form thing to do and write the code to support it. For example, you need to define a field in your form that's conditionally required. Perhaps with a simple true/false flag, or maybe you want something complicated like a callback/hook function.
Your code could look like

Code: Select all

$field = $form->addField("username", "text"); // name, type
$field->isRequired = true;
So you need two objects here: the form itself with an addField($name, $type) method, and something for the form field itself with an $isRequired property. Write that code, test it with a small form, fix any problems you find, and move on to the next requirement.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Problem with hyphens in array keys

Post by mecha_godzilla »

Thanks for your suggestion - what I've done with my code so far is define a set of form values like this:

Code: Select all

protected $form_array = array();
protected $form_error_counter = 0;

function __construct() {
    $this->populate_form_array('first_name','First Name:','text','50','yes');
}
then in the main part of the code I call:

Code: Select all

$this->parse_form_array('post');
and convert the form values to variables with the code I originally posted:

Code: Select all

foreach($this->form_array as $key => $value) {
    $val = $key;
    $$val = $value['value'];
}
Please note that I'm not saying this implementation is better or worse than your suggestion, just that this is how my mind works :mrgreen: The values in the form array include the element name, the form label for that element, what type checking is to be applied (currently just text, integer and date), a maximum character length and whether the value is mandatory or not. This works pretty much as I want it now but the above arrangements don't work so well when dealing with hyphens in the array keys. I'm sure there must be some way to escape them in my code so I'll keep looking...

Thanks,

M_G
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem with hyphens in array keys

Post by requinix »

You can code however you'd like except for one thing: don't use variable variables. Find another way. Probably using an array or function but it could be something else - as long as it's not variable variables.
Post Reply