Question

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!

Était-ce utile?

La solution

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";

Autres conseils

like this

$inc_folders = array(
    'default' => "def_folder"
);

$inc_folders['file']= $inc_folders['default'] . "/textfile.txt";

Here is another way, just not how you are currently trying:

$inc_folders['default'] = "def_folder";
$inc_folders['file']    = $inc_folders['default'] . "/textfile.txt";
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top