在数组中包含文件

时间:2013-01-23 22:53:08

标签: php arrays include

我有这段代码

$__routes = array(

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

);

我有像这样的example.php文件

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

我想在“$ __ routes array”

中包含example.php文件

有点像

$__routes = array(

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

include 'example.php';

);

请问我该怎么做?

2 个答案:

答案 0 :(得分:3)

不是没有改变example.php。每个文件本身必须是有效的PHP代码,因为include只发生在运行时(即达到这个确切的代码行),而不是在解析时(即文件加载时)

实现目标的一种方法是

<强>使用example.php

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

主文件

$__routes = array(
    "Home"                    => "index.php",
    "Contact"                 => "contact.php",
    "Register"                => "register.php"
) + (include 'example.php');

答案 1 :(得分:0)

如果我正确理解你的意图,你可以将一个文件包含在另一个文件中并合并数组:

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

$array2等于:

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