Output (echo/print) everything from a PHP Array

后端 未结 9 840
無奈伤痛
無奈伤痛 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 17:08

    var_dump() can do this.

    This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure.

    http://php.net/manual/en/function.var-dump.php

    0 讨论(0)
  • 2020-12-06 17:08
     //@parram $data-array,$d-if true then die by default it is false
     //@author Your name
    
     function p($data,$d = false){
    
         echo "<pre>"; 
             print_r($data);
         echo "</pre>"; 
    
         if($d == TRUE){
            die();
         } 
    } // END OF FUNCTION
    

    Use this function every time whenver you need to string or array it will wroks just GREAT.
    There are 2 Patameters
    1.$data - It can be Array or String
    2.$d - By Default it is FALSE but if you set to true then it will execute die() function

    In your case you can use in this way....

    while($row = mysql_fetch_array($result)){
        p($row); // Use this function if you use above function in your page.
    }
    
    0 讨论(0)
  • 2020-12-06 17:11

    If you want to format the output on your own, simply add another loop (foreach) to iterate through the contents of the current row:

    while ($row = mysql_fetch_array($result)) {
        foreach ($row as $columnName => $columnData) {
            echo 'Column name: ' . $columnName . ' Column data: ' . $columnData . '<br />';
        }
    }
    

    Or if you don't care about the formatting, use the print_r function recommended in the previous answers.

    while ($row = mysql_fetch_array($result)) {
        echo '<pre>';
        print_r ($row);
        echo '</pre>';
    }
    

    print_r() prints only the keys and values of the array, opposed to var_dump() whichs also prints the types of the data in the array, i.e. String, int, double, and so on. If you do care about the data types - use var_dump() over print_r().

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