如何从 PHP 文件加载返回数组?

我有一个 PHP 文件一个配置文件来自一个 消息翻译文件,其中包含:

<?php
return array(
'key' => 'value'
'key2' => 'value'
);
?>

我想从另一个文件加载这个数组并将其存储在一个变量中

我试过了,但没用

function fetchArray($in)
{
include("$in");
}

$in是 PHP 文件的文件名

有什么想法吗?

89738 次浏览

When an included file returns something, you may simply assign it to a variable

$myArray = include $in;

See http://php.net/manual/function.include.php#example-126

Returning values from an include file

We use this in our CMS. You are close, you just need to return the value from that function.

function fetchArray($in)
{
if(is_file($in))
return include $in;
return false
}

See example 5# here

As the file returning an array, you can simply assign it into a variable

Here is the example

$MyArray = include($in);
print_r($MyArray);

Output:

Array
(
[key] => value
[key2] => value
)