Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read an array from another php page

Tags:

arrays

file

php

I have the below mentioned code (actually, configuration) written in a file (config.php) and i want to get the content written in config.php in another file (check.php)

Codes written in config.php:

<?php
$CONFIG = array (
  'id' => 'asd5646asdas',
  'dbtype' => 'mysql',
  'version' => '5.0.12',
);

Code written in check.php to get the content is:

$config_file_path = $_SERVER['DOCUMENT_ROOT'] . 'config.php';
$config = file_get_contents($config_file_path);

Using the above code i am getting the output as a string and i wanted to convert it into an array. To do so, i have tried the below code.

$config = substr($config, 24, -5); // to remove the php starting tag and other un-neccesary things

$config = str_replace("'","",$config);
$config_array = explode("=>", $config);

Using the above code, i am getting an output like:

Array
(
    [0] =>   id 
    [1] =>  asd5646asdas,
  dbtype 
    [2] =>  mysql,
  version 
    [3] =>  5.0.12
)

which is incorrect.

Is there any way to convert it into an array. I have tried serialize() as well as mentioned in accessing array from another page in php , but did not succeed.

Any help on this will be appreciated.

like image 957
Debashis Avatar asked May 15 '26 04:05

Debashis


1 Answers

You don't need file_get_contents:

require_once 'config.php'
var_dump($CONFIG);
like image 160
Shuro Avatar answered May 16 '26 17:05

Shuro