Display an array in a readable/hierarchical format

后端 未结 18 955
北荒
北荒 2020-12-02 04:53

Here is the code for pulling the data for my array



        
相关标签:
18条回答
  • 2020-12-02 05:32

    print_r() is mostly for debugging. If you want to print it in that format, loop through the array, and print the elements out.

    foreach($data as $d){
      foreach($d as $v){
        echo $v."\n";
      }
    }
    
    0 讨论(0)
  • 2020-12-02 05:32
    echo '<pre>';
    foreach($data as $entry){
        foreach($entry as $entry2){
            echo $entry2.'<br />';
        }
    }
    
    0 讨论(0)
  • 2020-12-02 05:34
    <?php 
    //Make an array readable as string
    function array_read($array, $seperator = ', ', $ending = ' and '){
          $opt = count($array);
          return $opt > 1 ? implode($seperator,array_slice($array,0,$opt-1)).$ending.end($array) : $array[0];
    }
    ?>
    

    I use this to show a pretty printed array to my visitors

    0 讨论(0)
  • 2020-12-02 05:40

    Very nice way to print formatted array in php, using the var_dump function.

     $a = array(1, 2, array("a", "b", "c"));
     var_dump($a);
    
    0 讨论(0)
  • 2020-12-02 05:42

    Try this:

    foreach($data[0] as $child) {
       echo $child . "\n";
    }
    

    in place of print_r($data)

    0 讨论(0)
  • 2020-12-02 05:42

    I think that var_export(), the forgotten brother of var_dump() has the best output - it's more compact:

    echo "<pre>";
    var_export($menue);
    echo "</pre>";
    

    By the way: it's not allway necessary to use <pre>. var_dump() and var_export() are already formatted when you take a look in the source code of your webpage.

    0 讨论(0)
提交回复
热议问题