Include file in array

2019-02-19 23:58发布

I have this code

$__routes = array(

"Home"                    => "index.php",
"Contact"                 => "contact.php",
"Register"                => "register.php",

);

and i have example.php file like this

"Support"                 => "support.php",
"Success"                 => "success.php",
"Act"                     => "activate.php",

I want to include example.php file in "$__routes array"

Something to be like

$__routes = array(

"Home"                    => "index.php",
"Contact"                 => "contact.php",
"Register"                => "register.php",

include 'example.php';

);

Please how i can do that ?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-02-20 00:25

If I'm understanding your intention correctly, You could include one file into the other one and merge the arrays:

$merged_aray = array_merge($__routes, $array2);

Where $array2 equals:

"Support"                 => "support.php",
"Success"                 => "success.php",
"Act"                     => "activate.php"
查看更多
Fickle 薄情
3楼-- · 2019-02-20 00:27

Not without changing example.php. Each file must be valid PHP code on its own, because the include only happens at runtime (i.e. when this exact line of code is reached), not at parse time (i.e. when the file is loaded)

One way to accomplish what you need would be

example.php

return array(
    "Support"                 => "support.php",
    "Success"                 => "success.php",
    "Act"                     => "activate.php"
);

main file

$__routes = array(
    "Home"                    => "index.php",
    "Contact"                 => "contact.php",
    "Register"                => "register.php"
) + (include 'example.php');
查看更多
登录 后发表回答