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?
Use the fs. readFileSync() method to read a text file into an array in JavaScript, e.g. const contents = readFileSync(filename, 'utf-8'). split('\n') . The method will return the contents of the file, which we can split on each newline character to get an array of strings.
In PHP you can return one and only one value from your user functions, but you are able to make that single value an array, thereby allowing you to return many values.
If return is used outside of a function, it stops PHP code in the file from running. If the file was included using include , include_once , require or require_once , the result of the expression is used as the return value of the include statements.
A file with the . php file extension is a plain-text file that contains the source code written in the PHP (it's a recursive acronym meaning PHP: Hypertext Preprocessor) programming language. PHP is often used to develop web applications that are processed by a PHP engine on the web server.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With