0

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!

3 Answers 3

5

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";
Sign up to request clarification or add additional context in comments.

Comments

1

like this

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

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

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.