How to load return array from a PHP file?

PhpArraysInclude

Php Problem Overview


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?

Php Solutions


Solution 1 - Php

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

Solution 2 - Php

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

Solution 3 - Php

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
)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbmanView Question on Stackoverflow
Solution 1 - PhpPhilView Answer on Stackoverflow
Solution 2 - PhpJasonView Answer on Stackoverflow
Solution 3 - PhpNishad UpView Answer on Stackoverflow