I have a php file that contains an array $myArray
.
<?php
$myArray = array(
'key1'=>'value1',
'key2'=>'value2',
);
?>
I need to read the value of key1
from that array. Is there a way to directly read key1
, without including the whole file? Right now I'm using include like this.
$includedArray = include('/path/to/myArray.php');
but this is creating a problem for me, because even though I include it under a new name $includedArray
, it still recognizes the old name $myArray
which causes naming conflicts.
To solve this problem, I would have changed the included array from a named array ($myArray) to an un-named array, but I can't make changes to the included files. So is there a way to either:
include the file containing the named array, but have it completely forget the original name ($myArray
), and have it use ONLY the new name I give it ($includedArray
)?
or is there a way to simply read 1 key from the array without including the whole file?
Then copy the array to another variable and unset the original array?
path/to/myNewArray.php:
return call_user_func(function() {
include "/path/to/myArray.php";
return $myArray;
});
/*
if (isset($myArray)) {
$tmpMyArray = $myArray; // storing the $myArray if defined
}
$includedArray = $myArray;
unset($myArray);
if (isset($tmpMyArray)) {
$myArray = $tmpMyArray; // restoring the previous $myArray
}
*/
usage:
$whatEver = include("/path/to/myNewArray.php"); // no interference now
Take advantage of function scope
$myArray = arrayInclude("myArray.php");
var_dump($myArray);
function arrayInclude($file)
{
include $file;
return $myArray;
}
Output
array
'key1' => string 'foo1' (length=4)
'key2' => string 'foo2' (length=4)
myArray.php
$myArray = array (
'key1' => 'foo1',
'key2' => 'foo2'
);
Using function
& namespace
a.php
include 'b.php';
include 'c.php';
$myArray = call_user_func ( 'b\myArray' );
var_dump ( $myArray );
$myArray = call_user_func ( 'c\myArray' );
var_dump ( $myArray );
Output
array
'key1' => string 'foo1' (length=4)
'key2' => string 'foo2' (length=4)
array
'key1' => string 'bar1' (length=4)
'key2' => string 'bar2' (length=4)
b.php
namespace b;
function myArray() {
return array (
'key1' => 'foo1',
'key2' => 'foo2'
);
}
c.php
namespace c;
function myArray() {
return array (
'key1' => 'bar1',
'key2' => 'bar2'
);
}
If you need shared values but you don't want to use global variables shared with a common included php config file, what about storing those values in a xml or json file?
With Json you can load the "array" from a file into a variable of your choice.
http://www.php.net/manual/en/function.json-decode.php
Or you could use output buffers, but that does not apply very well for your current use case.
function ob_include_to_string($filename)
{
ob_start(); // Starts the 'output buffer'
include($filename); // Includes the file
$return_variable = ob_get_contents(); // Sets an variable to keep the content of the echo'ed out content
ob_end_clean(); // Ends and deletes the 'output buffer'; "cleans it up"
return $return_variable; // Returns the variable with the content
}
What about changing your config file to:
<?php
$myArray = array(
'key1'=>'value1',
'key2'=>'value2',
);
echo json_encode($myArray);
?>
And then you could do:
$newArray = json_decode(ob_include_to_string('config.php'));
array.php
<?php
class MyArray {
public static $myArray = array('key1' => value1, 'key2' => value2);
}
?>
other.php
<?php
include('array.php');
//Access the value as
MyArray::$myArray['key1'];
?>