Reputation: 256
Having an array, I want to call a value giving a key within the same array!
In my following example, I have tried to call a particular value giving the key 'default'
within the same array, but without success! Is it possible to do that in PHP? Here is my array:
$inc_folders = array(
'default' => "def_folder",
'file' => $inc_folders['default'] . "/textfile.txt"
);
calling the value in $inc_folders['file']
, I would want the result be the following: "def_folder/textfile.txt"
; but I obtain an error!
Please, can you help me to resolve that?
Many thanks!
Upvotes: 0
Views: 158
Reputation: 78994
Here is another way, just not how you are currently trying:
$inc_folders['default'] = "def_folder";
$inc_folders['file'] = $inc_folders['default'] . "/textfile.txt";
Upvotes: 0
Reputation: 8872
like this
$inc_folders = array(
'default' => "def_folder"
);
$inc_folders['file']= $inc_folders['default'] . "/textfile.txt";
Upvotes: 1
Reputation: 19552
You need to assign it to a variable first; like so:
$folder = "def_folder";
$inc_folders = array(
'default' => $folder,
'file' => $folder . "/textfile.txt"
);
Or alternatively, build the array in two steps:
$inc_folders = array(
'default' => "def_folder"
);
$inc_folders['file'] = $inc_folders['default'] . "/textfile.txt";
Upvotes: 5