The purpose of writing this method is that I want to read a PHP array configuration file into the program, add some configuration, and then write the file;
The Var_export method will disrupt the original configuration (such as the array sequence number I did not add, he automatically added to me 0,1,2,3 ...), and the format is not good to see (indentation is very ugly).
Method and Application
<?php$arr=array (' gii ' =>array (' class ' = ' system.gii.GiiModule ', ' password ' = ' rd6688 ', ' ipfilters ' + = Array (' 127.0.0.1 ', ':: 1 '), ' generatorpaths ' = = Array (' Ext.bootstrap-theme.gii ',),), function Arr2str ($arr, $t =0) {$str = null, $pad = Str_pad ("", $t, "\ T"), foreach ($arr as $k + = $v) {if (Is_array ($v)) {if (is_string ($k)) {$str. = $pad. "". $k. "' =>array (\ n ". Arr2str ($v, $t + 1). $pad."), \ n ";} else{$str. = $pad. " Array (\ n ". Arr2str ($v, $t + 1). $pad."), \ n ";}} Else{if (is_string ($k)) {$str. = $pad. "'". $k. "' = ' ". $v." ', \ n ";} else{$str. = $pad. "'". $v. "', \ n ';}}} return $STR;} Apply echo "Array (\ n"; Echo arr2str ($arr, $t =1); echo ")";? >
Execution results
Array (' gii ' =>array (' class ' = ' system.gii.GiiModule ', ' password ' = ' rd6688 ', ' ipfilters ' =>array (' 127.0.0.1 ', ':: 1 ',), ' Generatorpaths ' =>array (' ext.bootstrap-theme.gii ',),)
If you write to a file, do not use echo to write directly.
The array format is preserved after the PHP array goes to the string