Display an array in a readable/hierarchical format

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

Here is the code for pulling the data for my array



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

    For single arrays you can use implode, it has a cleaner result to print.

    <?php
    $msg = array('msg1','msg2','msg3');
    echo implode('<br />',$msg);
    echo '<br />----------------------<br/>';
    
    echo nl2br(implode("\n",$msg));
    echo '<br />----------------------<br/>';
    ?>
    

    For multidimensional arrays you need to combine with some sort of loop.

    <?php
    $msgs[] = $msg;
    $msgs[] = $msg;
    $msgs[] = $msg;
    $msgs[] = $msg;
    $msgs[] = $msg;
    foreach($msgs as $msg) {
        echo implode('<br />',$msg);
        echo '<br />----------------------<br/>';
    }
    ?>
    
    0 讨论(0)
  • 2020-12-02 05:45

    I have a basic function:

    function prettyPrint($a) {
        echo "<pre>";
        print_r($a);
        echo "</pre>";
    }
    
    prettyPrint($data);
    

    EDIT: Optimised function

    function prettyPrint($a) {
        echo '<pre>'.print_r($a,1).'</pre>';
    }
    

    EDIT: Moar Optimised function with custom tag support

    function prettyPrint($a, $t='pre') {echo "<$t>".print_r($a,1)."</$t>";}
    
    0 讨论(0)
  • 2020-12-02 05:47

    This may be a simpler solution:

    echo implode('<br>', $data[0]);
    
    0 讨论(0)
  • 2020-12-02 05:47

    I use this for getting keys and their values $qw = mysqli_query($connection, $query);

    while ( $ou = mysqli_fetch_array($qw) )
    {
        foreach ($ou as $key => $value) 
        {
                echo $key." - ".$value."";
        }
        echo "<br/>";
    }
    
    0 讨论(0)
  • 2020-12-02 05:47

    I would just use online tools.

    • first do print_r(your_array)

    • Second copy the result and paste in http://phillihp.com/toolz/php-array-beautifier/

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

    if someone needs to view arrays so cool ;) use this method.. this will print to your browser console

    function console($obj)
    {
        $js = json_encode($obj);
        print_r('<script>console.log('.$js.')</script>');
    }
    

    you can use like this..

    console($myObject);
    

    Output will be like this.. so cool eh !!

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