Php return array from file

How to load return array from a PHP file?

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

 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

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 > 

Solution 3 — Php

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

$MyArray = include($in); print_r($MyArray); 
Array ( [key] => value [key2] => value ) 

How can I disable all views inside the layout?

The right place to keep my signals.py file in a Django project

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 Type Original Author Original Content on Stackoverflow
Question bman View Question on Stackoverflow
Solution 1 — Php Phil View Answer on Stackoverflow
Solution 2 — Php Jason View Answer on Stackoverflow
Solution 3 — Php Nishad Up View Answer on Stackoverflow

Источник

PHP: Return array from include file.

This is a short PHP guide on how to return an array from an included file. This can be particularly useful for setting up configuration files and the like.

Take a look at the following PHP file, which I have named file.php:

The file above returns an array with three elements in it.

Now, let’s say that we want to include this file and assign the array to a variable in our PHP script:

In the code snippet above, we included the file and assigned its return value to a PHP variable.

If you var_dump the $myArray variable, the result will be as follows:

As you can see, $myArray contains the array that we returned from our included file.

Note that your include file can return any kind of variable. It does not have to be an array. If you want to, your file can return an object, string or integer. This all depends on what you are trying to achieve.

Returning from an include file particularly handy if you want to return different configuration values. For example, you could include a different file depending on what database server you want to connect to:

$databaseDetails = false; //If it's a production server. if(PRODUCTION) < //Include production.php $databaseDetails = include 'production.php'; >else < //Otherwise, include the development.php database details. $databaseDetails = include 'development.php'; >//Connect to DB using $databaseDetails.

Hopefully, these examples helped you on your way!

Источник

PHP load array values from file and return them as array

Or you use the $object directly if it is set in file1.php file2.php Solution 2: You can do this by using $_SESSION or $_COOKIE, See here for more detail; PHP Pass variable to next page Solution 3: Be careful at the variable scope. Solution 1: Ing. Michal Hudak Your code is correct Solution 2: you can simply do a in your en.php file and then assign it to a variable in the load function:

PHP load array values from file and return them as array

you can simply do a return $lang in your en.php file and then assign it to a variable in the load function: $load = include $file . ‘.php’;

lang['word'] = 'word'; $this->lang['text'] = 'text'; > public function get_lang() < return $this->lang; > > ?> 
get_lang(); echo $get_lang['word']; echo $get_lang['text']; > load('en'); ?> 

Untested & it’s early — but this should work.

How to load return array from a PHP file?, I have a PHP file a configuration file coming from a Yii message translation file which contains this: ‘value’ ‘key2’ => ‘value’ ); ?> I want to load this array from another file and store it in a variable Usage example$myArray = include $in;Feedback

Get Array from PHP file and use in another PHP file

You have to set a variable or something, not echoing it.

Or you use the $object directly if it is set in file1.php

include('path/file1.php'); echo "
delivery['firstname']) ? $order->delivery['firstname'] : '') . "
";

You can do this by using $_SESSION or $_COOKIE, See here for more detail; PHP Pass variable to next page

Be careful at the variable scope. See this link: http://php.net/manual/en/language.variables.scope.php

require_once('path/file1.php'); global $order; print_r($order->delivery); 

Defining $order as global should fix your issue.

PHP load array values from file and return them as array, How to process file to return each array value in load () to use it in index.php like this: echo $lang [‘word’]; I know that include function view file in …

Call php file to return an array, and load it into a combo box

You can use something similar to this:

function get_data() < // May be you want to load data from DB // This is just a hint return array('key1' =>'Value 1', 'key2' => 'Value 2'); > 

If you want to do it without reloading the page, you’ll need to look into AJAX. If you want to do this at the time you request the page, you can include the page and call any functions out of it.

Get Array from PHP file and use in another PHP file, So i am having trouble getting an array from one PHP file to another. In my first file (file1.php) i got following code: You could return an array in a file and use it in another like this:

How to load array from php file using codeigniter?

Another way to do it would be to create a file like this (call it «data.php»):

 12, [ 'nome' ] => "Acre", [ 'sigla' ] => "AC" ), array( [ 'id' ] => 27, [ 'nome' ] => "Alagoas", [ 'sigla' ] => "AL" ), array( [ 'id' ] => 16, [ 'nome' ] => "Amapa", [ 'sigla' ] => "AP" ), array( [ 'id' ] => 13, [ 'nome' ] => "Amazonas", [ 'sigla' ] => "AM" ), array( [ 'id' ] => 29, [ 'nome' ] => "Bahia", [ 'sigla' ] => "BA" ), // and more. ); 
$data = require("/path/to/data.php"); 

If it’s static content, you can just load it in the controller action.

public function index() < $data['title'] = 'Page Title'; $data['metaDesc'] = 'meta tags'; $data['metaKeywords'] = ''; $this->load->view('articles/view', $data); >

Call php file to return an array, and load it into a combo, load_data.php function get_data () < // May be you want to load data from DB // This is just a hint return array ('key1' =>‘Value 1’, ‘key2’ => ‘Value 2’); > And in your main file:

Источник

How to Load Return Array from a PHP File

Get Array from PHP file and use in another PHP file

You have to set a variable or something, not echoing it.

include('path/file1.php');
echo "
" . (isset($delivery['firstname']) ? $delivery['firstname'] : '') . "
";

Or you use the $object directly if it is set in file1.php

include('path/file1.php');
echo "
" . (isset($order->delivery['firstname']) ? $order->delivery['firstname'] : '') . "
";

How to read array from text file?

If you plan on reusing those same values inside the array, you could use var_export on creating that array file instead.

$arr = array('name','rollno','address');
file_put_contents('array.txt', '

Then, when the time comes to use those values, just use include :

$my_arr = include 'array.txt';
echo $my_arr[0]; // name

Or just use a simple JSON string, then encode / decode:

$arr = array('name','rollno','address');
file_put_contents('array.txt', json_encode($arr));
$my_arr = json_decode(file_get_contents('array.txt'), true);
echo $my_arr[1]; // rollno

PHP load array values from file and return them as array

function load($file) include $file . '.php'; 
return $lang;
>

print_r(load('en'));

Append array entry to php file that returns an array

You can use var_export function for that


$tmp_array = include 'file_1.php';
$tmp_array["key3"] = "value3";
file_put_contents("file_1.php","");

?>
 return [ 
'key1' => 'value1',
'key2' => 'value2',
];
?>
return array ( 
'key1' => 'value1',
'key2' => 'value2',
'key3' => 'value3',
);
?>

Read an array from a php file

You don’t need file_get_contents() for the goal you intend to achieve since the file contains a declared Variable. What you need is include the file in your Current Script and access the Array as if it was declared in the same script as shown below. Remember that file_get_contents() will always return the contents of a File as a String. Ideally (in this case), this is not what you need.

 // CURRENT SCRIPT: --- PSUEDO FILE-NAME: test.php 
require_once $filepath; // //
// JUST AS A DOUBLE-CHECK, VERIFY THAT THE VARIABLE $someArray EXISTS
// BEFORE USING IT. HOWEVER, SINCE YOU ARE SURE IT DOES EXIST,
// YOU MAY WELL SKIP THIS PART AND SIMPLY START USING THE $someArray VARIABLE.
if(isset($someArray)) var_dump($someArray);
>

Источник

Читайте также:  Велосипед stinger 29 python std зеленый алюминий размер 22
Оцените статью