PHP Classes

save array as return array

Recommend this page to a friend!

      Config Editor  >  All threads  >  save array as return array  >  (Un) Subscribe thread alerts  
Subject:save array as return array
Summary:how to save array in file with return array();
Messages:2
Author:mirkp
Date:2014-09-19 05:01:02
 

 


  1. save array as return array   Reply   Report abuse  
Picture of mirkp mirkp - 2014-09-19 05:01:02
Hi

currently it save config file with $my_arra_name = array();

I want to save file in this way:

return array();

so I can use config file in this way:

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

thanks!!

  2. Re: save array as return array   Reply   Report abuse  
Picture of mirkp mirkp - 2014-09-19 05:25:25 - In reply to message 1 from mirkp
I was able to do this.

I change method Save() in this way:

public function Save($fname = false) {

$src = "<?php ".CE_NEWLINE."return array(".CE_NEWLINE;

foreach ($this->vars as $var) {
$src .= self::formatComments(wordwrap($var[CE_VARCOMMENT], CE_WORDWRAP, CE_NEWLINE));
$src .= "'".$var[CE_VARNAME]."' => ".$var[CE_VARVALUE].','.CE_NEWLINE.CE_NEWLINE;

//$src .= 'return '.$var[CE_VARVALUE].';'.CE_NEWLINE.CE_NEWLINE;
}
$src = $src.'); ?>';

if ($fname !== false) {
$fp = @fopen($fname, 'w');
if ($fp) {
@fwrite($fp, $src);
@fclose($fp);
}
}
return $src;
}


Result of generated file are that var name become index key of one main array.

This is generated config sample:


<?php
return array(
// Timeout in cURL
// operations
'cfg_curl_timeout' => 10,

// Banned IPs
'cfg_banned_ips' => array (
0 => '10.10.10.10',
1 => '10.20.30.40',
),

// ROOTDIR is defined
// before including
// config file
'cfg_cachedir' => ROOTDIR.'/maincache',

); ?>

 
For more information send a message to info at phpclasses dot org.