How to load return array from a PHP file?
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
Get Array from PHP file and use in another PHP file
You have to set a variable or something, not echoing it.
file1.php:
$delivery = $order->delivery;
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($delivery['firstname']) ? $delivery['firstname'] : '') . "</div>";
Or you use the $object directly if it is set in file1.php
file2.php
include('path/file1.php');
echo "<div id='test'>" . (isset($order->delivery['firstname']) ? $order->delivery['firstname'] : '') . "</div>";
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.
Basic example:
$arr = array('name','rollno','address');
file_put_contents('array.txt', '<?php return ' . var_export($arr, true) . ';');
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));
Then, when you need it:
$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
Ing. Michal Hudak
Your code is correct
<?php
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
<?php
$tmp_array = include 'file_1.php';
$tmp_array["key3"] = "value3";
file_put_contents("file_1.php","<?php\nreturn ".var_export($tmp_array, true).";\n?>");
?>
Its working for me
Before:
<?php
return [
'key1' => 'value1',
'key2' => 'value2',
];
?>
After:
<?php
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 thatfile_get_contents()
will always return the contents of a File as a String. Ideally (in this case), this is not what you need.
<?php
// CURRENT SCRIPT: --- PSUEDO FILE-NAME: test.php
require_once $filepath; //<== $filepath BEING THE PATH TO THE FILE CONTAINING
//<== YOUR DECLARED ARRAY: $someArray
// 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);
}
Related Topics
Passing JavaScript Variables to PHP
Scrape Web Site Generated by JavaScript
Array Merge on Multidimensional Array
List of Us Time Zones for PHP to Use
How Is Output(Echo) Array Without Use of JSON_Encode(Codeigniter)
Symfony2 Collection of Entities - How to Add/Remove Association with Existing Entities
Magento Addfieldtofilter: Two Fields, Match as Or, Not And
PHP File Uploads Doesnot Read $_Files['Image']
Display Single Column Value of MySQLi Query
How to Remove a Key and Its Value from an Associative Array
How to Trigger Xdebug Profiler for a Command Line PHP Script
How to Write to the Console from a Laravel Controller
A Script to Change All Tables and Fields to the Utf-8-Bin Collation in MySQL