Home > php教程 > PHP源码 > body text

将php数组还原成php源代码

PHP中文网
Release: 2016-05-25 17:07:18
Original
1516 people have browsed it

1. [代码][PHP]代码    

<?php
    //说明:本程序将用于php代码中的数组调试,由于工作原因,经常要将php数组json化后传给客户端javascript,但是这些php数组是否是
    //符合格式的json字符串。往往数组传递过去了,但是前台没按要求显示出来。很可能是json_encode($arr)中的$arr格式不对,这时候用
    //var_dump($arr)打印出来的不是php代码的数组,于是自己写了这个功能,用于将php数组还原成php源码,这样就方便复制后修改,调试了。
    //--------------------array to code---------------------------start
    //四个函数
    //function is_assoc_array($array);判断是否是关联数组
    //function array_index_code(); 返回索引字符串源码
    //function array_assoc_code(); 返回关联字符串源码
    function is_assoc_array($array){ return array_keys($array) !== range(0, count($array) - 1);}

    /*打印如下字符串[索引数组源码]:
        array(
            "111",
            &#39;222&#39;,
            "333",
        )
    */
    function array_index_code($arr,$tab){
        $code = str_repeat("\t",$tab). "array(\n";
        if( $arr ){
            foreach( $arr AS $v){
                if(is_int($v)){
                    $code .= str_repeat("\t",$tab). "$v,\n";
                }elseif(is_array($v)){
                    $code .= array_code($v,$tab); //递归调用.索引数组不需要用tab+2
                }else{
                    $code .= str_repeat("\t",$tab). "&#39;$v&#39;,\n";
                }
            }
        }
        $code .= str_repeat("\t",$tab). "),\n"; //尾部有逗号
        return $code;
    }	
	/*打印如下字符串[关联数组源码]:
        array(
            "aaa"=>"111",
            "bbb"=>&#39;222&#39;,
            "ccc&#39;=> array(
                    ),
        )
    */
    function array_assoc_code($arr,$tab){
        $code = str_repeat("\t",$tab). "array(\n";
        if( $arr ){
            foreach( $arr AS $k=>$v){
                if(is_int($v)){
                    $code .= str_repeat("\t",$tab). "&#39;$k&#39;=>$v,\n";
                }elseif(is_array($v)){
                    $code .= str_repeat("\t",$tab). "&#39;$k&#39;=>\n". array_code($v,$tab+2); //递归调用,关联数组需要用tab+2
                }else{
                    $code .= str_repeat("\t",$tab). "&#39;$k&#39;=>&#39;$v&#39;,\n";
                }
            }
        }
        $code .= str_repeat("\t",$tab). "),\n"; //尾部有逗号
        return $code;
    }

	/*
		打印数组,打印成php代码
	*/
    function array_code($arr,$tab=0){
        if(is_assoc_array($arr)){
            return array_assoc_code($arr,$tab);
        }else{
            return array_index_code($arr,$tab);
        }
    }
    //--------------------array to code---------------------------end
	//测试例子
	$arrTEST = array(
		"aaa"=>"111",
		"bbb"=> array(
				1,
				3,
				array(
					"wu"=>1,
					"jun"=>2,
					"jie"=>array(
						"aaa"=>"bbb",
						"ccc"=>"ddd",
					),
				)
				),
	);
	//var_dump($arrTEST);die;
	echo array_code($arrTEST);
?>
Copy after login

                   

                   

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!