How to load return array from a PHP file?

2019-01-16 13:45发布

I have a PHP file a configuration file coming from a Yii message translation file which contains this:

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

I want to load this array from another file and store it in a variable

I tried to do this but it doesn't work

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

$in is the filename of the PHP file

Any thoughts how to do this?

2条回答
仙女界的扛把子
2楼-- · 2019-01-16 13:59

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

查看更多
聊天终结者
3楼-- · 2019-01-16 14:11

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

查看更多
登录 后发表回答