The array format is preserved after the PHP array goes to the string

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.