【问题标题】:Include file in array在数组中包含文件
【发布时间】:2013-01-07 14:14:14
【问题描述】:

我有这个代码

$__routes = array(

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

);

我有这样的 example.php 文件

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

我想在“$__routes 数组”中包含 example.php 文件

有点像

$__routes = array(

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

include 'example.php';

);

请问我该怎么做?

【问题讨论】:

    标签: php arrays include


    【解决方案1】:

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

    实现您需要的一种方法是

    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');
    

    【讨论】:

      【解决方案2】:

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

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

      $array2 等于:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-18
        • 1970-01-01
        • 1970-01-01
        • 2013-01-07
        • 2015-07-20
        • 2020-02-05
        • 2023-04-06
        • 1970-01-01
        相关资源
        最近更新 更多