Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array keys - what's allowed?

Status
Not open for further replies.

Foamcow

Programmer
Nov 14, 2002
6,092
GB
I'm working on a little calculation script at the moment and for reasons I won't go into I've come across what I think might be a bit of a design faux pas.

I have an associative array which is along the lines of

Code:
['Our Fees']=>350,
['Registry fees'=>60,
['Search fee']=>30

The array keys are used when outputting a table of charges

I was concerned over the use of spaces but everything seems to work fine.

Now, I need to do some more work and allow for the use of discount codes. This is not a problem except that I have added a bit of HTML to the output of the array key.
Again, this works fine.

However, there comes a point where this data may be stored in a database table that is set up to store each charge in a row with columns holding the key and the value.

Retrieving these charges and displaying the result is fine but I am concerned that the key value with the HTML is going to end up being used as an array key.

My suspicion is that having the HTML in the array key is really going to mess it up.

After all that (had to get it straight in my head) I think my question is, wait for it...

Are there restrictions on what characters can be used for array keys or on their length?



--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
i'm not aware of any restrictions over what strings may be used as an associative array key.
 
You know, I'm so glad you said that :)

Like I said, it's all working but there's something nagging at the back of my mind.

I think I'll let it go for now and go back to it if I have a revelation or it dies.

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
$myvar['<input type=text value="hello">']="Some value"

This would be perfectly valid, however I'm not sure how practical it would be to have to reference all that.
Also not sure a database would take all that as a field name and not at all sure why you would want to do it anyway.

What benefit are you obtaining from storing html as an array key? Would be the better question


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
i agree with vacunita. if you must go down this route, consider keeping a mapping of dbase to array keys and use more standard methods of naming columns.

 
Oh the columns are named normally with nothing odd going in there.

I have a table for quotes and another table for charges.
The charges table contains a column with the quote id that it relates to.

The array key is stored as a string in a column of the charges table along with the value of the charge.

I can then select charges from the table based on the quote id and output using a simple foreach to output the name of the charge and how much is charged.

Code:
SELECT c.chargestring, c.charge from charges c WHERE c.quoteid=$somevalue
PHP:
foreach($data as $k=>$v){
  echo("{$k = $v}");
}

The only time it gets awkward is when I want to apply a discount code. In this case the charge value is modified and the description gets a bit of HTML tagged onto it showing a discount has been applied.

The reason I did it like this is that the quote system has to be quite flexible, the type of charges vary based on what is being quoted for.

It's all working perfectly at the moment (barring a couple of bugs in the calculations - easily fixable), quote storage, retrieval, call back requests, discount codes all via AJAX calls (that will degrade nicely once I've finished). Hurrah!

--
Tek-Tips Forums is Member Supported. Click Here to donate

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top