Jump to content

assigning array key name by variable


Recommended Posts

To answer your question: variable variables

$key = "apple";
$arr = array($$key => "fruit");

This will throw an undefined variable error.

The OP's original code was correct, I suspect the issue is in the retrieval.

$key = "apple";
$arr = array($key => "fruit");
// Output: Array ( [apple] => fruit ) 

Link to comment
Share on other sites


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.