Output (echo/print) everything from a PHP Array

后端 未结 9 839
無奈伤痛
無奈伤痛 2020-12-06 16:21

Is it possible to echo or print the entire contents of an array without specifying which part of the array?

The scenario: I am trying to echo everything from:

<
相关标签:
9条回答
  • 2020-12-06 16:46

    You can use print_r to get human-readable output.

    See http://www.php.net/print_r

    0 讨论(0)
  • 2020-12-06 16:48

    You can use print_r to get human-readable output. But to display it as text we add "echo '';"

    echo ''; print_r($row);

    0 讨论(0)
  • 2020-12-06 16:50

    I think you are looking for print_r which will print out the array as text. You can't control the formatting though, it's more for debugging. If you want cool formatting you'll need to do it manually.

    0 讨论(0)
  • 2020-12-06 16:59

    Similar to karim's, but with print_r which has a much small output and I find is usually all you need:

    function PrintR($var) {
        echo '<pre>';
        print_r($var);
        echo '</pre>';
    }
    
    0 讨论(0)
  • 2020-12-06 17:01

    This is a little function I use all the time its handy if you are debugging arrays. Its pretty much the same thing Darryl and Karim posted. I just added a parameter title so you have some debug info as what array you are printing. it also checks if you have supplied it with a valid array and lets you know if you didn't.

    function print_array($title,$array){
    
            if(is_array($array)){
    
                echo $title."<br/>".
                "||---------------------------------||<br/>".
                "<pre>";
                print_r($array); 
                echo "</pre>".
                "END ".$title."<br/>".
                "||---------------------------------||<br/>";
    
            }else{
                 echo $title." is not an array.";
            }
    }
    

    Basic usage:

    //your array
    $array = array('cat','dog','bird','mouse','fish','gerbil');
    //usage
    print_array("PETS", $array);
    

    Results:

    PETS
    ||---------------------------------||
    
    Array
    (
        [0] => cat
        [1] => dog
        [2] => bird
        [3] => mouse
        [4] => fish
        [5] => gerbil
    )
    
    END PETS
    ||---------------------------------||
    
    0 讨论(0)
  • 2020-12-06 17:07

    For nice & readable results, use this:

    function printVar($var) {
        echo '<pre>';
        var_dump($var); 
        echo '</pre>';
    }
    

    The above function will preserve the original formatting, making it (more)readable in a web browser.

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