Battlefrisk Battlefrisk - 7 months ago 9
PHP Question

Include arrays with the same name from different files in PHP?

I am trying to figure out how I can include arrays that have the same name into my PHP file from two different files .

I know it's simple, but I can't seem to find the right search to get me to the solution.

I've been trying to reference the arrays such as

function_call(menu_config.$config)
but that just isn't working.

Answer

Assuming both files have a $config array. If you want one resulting array:

include('file1.php');
$new = $config;

include('file2.php');
$new = array_merge($new, $config);

Or even two new arrays:

include('file1.php');
$config_1 = $config;

include('file2.php');
$config_2 = $config;

Another option would be a function:

function get_config($file) {
    include($file);
    return $config;
}

$config_1 = get_config('file1.php');