开发者

echo a complex array and indent subarrays

I have a complex array, some开发者_Python百科thing like that:

Array {
        k1 => text1
        k2 => Array {
                      k3 =>text2
                      k4 => Array {
                                    k5 => text3
                                  }
                      k6 => text4
                    }
      }

And i want to echo the array but to indent every subarray like this:

key: k1 >> value: text1
Array key: k2 >> values:
    key: k3 >> value: text2
    Array key: k4 >> values:
        key: k5 >> value: text3
    key: k6 >> value: text4

Let me know if you need any details.


Here's a recursive function that will indent:

(Edited: Indentation wasn't working properly for all subelements, now it does)

function arrayPrettyPrint($arr, $level = 0) {
    foreach($arr as $k => $v) {
        for($i = 0; $i < $level; $i++)
            echo "&nbsp;";   // You can change how you indent here
        if(!is_array($v))
            echo($k . " => " . $v . "<br/>");
        else {
            echo($k . " => <br/>");
            arrayPrettyPrint($v, $level+1);
        }
    }
}

$arr = array(
    1, 2, 3,
array( 4, 5, 
    array( 6, 7, array( 8 )))
);

arrayPrettyPrint($arr);


It's print array like your.

<?php

function print_array($array, $tabs = '') {
    $result = '';
    if (is_array($array)) {
        foreach ($array as $k => $v) {
            if (is_array($v)) {
                $result .= $tabs . 'Array key: '. $k . ' >> values: ' . PHP_EOL . print_array($v, $tabs."\t");
            } else {
                $result .= $tabs . 'key: ' . $k . ' value: ' . $v . PHP_EOL;
            }
        }
    } else {
        $result = $array . PHP_EOL;
    }
    return $result;
}

$array = array(
    'k1' => 'text1',
    'k2' => array(
        'k3' => 'text2',
        'k4' => array(
            'k5' => 'text3'
        ),
        'k6' => 'text4'
    )
);

echo print_array($array);

?>


Try building on this quick function:

function recurseDisplay($my_array,$padding = 2){

   echo "<br />";
   foreach($my_array as $item){
     if (is_array($item)){
       $padding += 10;
       for ($p = 0; $p < $padding; $p++){
         echo "&nbsp;";
       }
       echo "Array: &nbsp; {" ;
       recurseDisplay($item,$padding);
       echo " <br /> } <br />";
     }
     else{
        for ($p = 0; $p < $padding; $p++){
         echo "&nbsp;";
       }
       echo "key :" . $item . "<br />";
     }
   }
 }

 recurseDisplay($my_array);


Would var_dump do the job for you?

 myVar = Array {
        k1 => text1
        k2 => Array {
                      k3 =>text2
                       k4 => Array {
                                    k5 => text3
                                  }
                      k6 => text4
                    }
      }

try it

var_dump(myVar)


It appears that this question was asked before here. This was the solution by KeithGrant

function RecursiveWrite($array) {
    foreach ($array as $vals) {
        echo "\t" . $vals['comment_content'];
        RecursiveWrite($vals['child']);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜