Pretty-Printing JSON with PHP

后端 未结 24 1869
一向
一向 2020-11-22 02:03

I\'m building a PHP script that feeds JSON data to another script. My script builds data into a large associative array, and then outputs the data using json_encode

24条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 03:06

    print_r pretty print for PHP

    Example PHP

    function print_nice($elem,$max_level=10,$print_nice_stack=array()){
        if(is_array($elem) || is_object($elem)){
            if(in_array($elem,$print_nice_stack,true)){
                echo "RECURSION";
                return;
            }
            $print_nice_stack[]=&$elem;
            if($max_level<1){
                echo "nivel maximo alcanzado";
                return;
            }
            $max_level--;
            echo "";
            if(is_array($elem)){
                echo '';
            }else{
                echo '';
            }
            $color=0;
            foreach($elem as $k => $v){
                if($max_level%2){
                    $rgb=($color++%2)?"#888888":"#BBBBBB";
                }else{
                    $rgb=($color++%2)?"#8888BB":"#BBBBFF";
                }
                echo '";
            }
            echo "
    ARRAY
    '; echo 'OBJECT Type: '.get_class($elem).'
    '; echo ''.$k.""; print_nice($v,$max_level,$print_nice_stack); echo "
    "; return; } if($elem === null){ echo "NULL"; }elseif($elem === 0){ echo "0"; }elseif($elem === true){ echo "TRUE"; }elseif($elem === false){ echo "FALSE"; }elseif($elem === ""){ echo "EMPTY STRING"; }else{ echo str_replace("\n","*
    \n",$elem); } }

提交回复
热议问题