Jump to content

Having trouble with array. Adding key => value when key is variable.


Anti-Moronic

Recommended Posts

Title says it all really. I have no idea if I am doing this correctly or effectively.

 

Here's my array:

 

<?php

$arr = array(

"key1" => "val1",


)

//To add a key => val I use:

$arr['key'] = "val";

?>

 

That works fine, but I can't assign a key=>val unless the key is literal and within single quotes. For example, this doesn't work:

 

$key=key2;

 

$arr[$key] = "val";

 

Any advice on how I should be going about this. Maybe a better way of adding elements to my array (while being able to set custom keys).

 

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.