从 PHP 数组输出(回显/打印)所有内容 [英] Output (echo/print) everything from a PHP Array
问题描述
是否可以在不指定数组的哪一部分的情况下回显或打印数组的全部内容?
场景:我试图回应以下所有内容:
while($row = mysql_fetch_array($result)){回声 $row['id'];}
不指定id",而是输出数组的完整内容.
如果你想自己格式化输出,只需添加另一个循环(foreach)来遍历当前行的内容:
while ($row = mysql_fetch_array($result)) {foreach ($row as $columnName => $columnData) {echo '列名:'.$columnName .'列数据:'.$columnData .'<br/>';}}
或者,如果您不关心格式,请使用之前答案中推荐的 print_r 函数.
while ($row = mysql_fetch_array($result)) {echo '';print_r ($row);echo '</pre>';}
print_r() 只打印数组的键和值,而 var_dump() 也打印数组中数据的类型,即 String、int、double 等.如果您确实关心数据类型 - 使用 var_dump() 而不是 print_r().
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:
while($row = mysql_fetch_array($result)){
echo $row['id'];
}
Without specifying "id" and instead outputting the complete contents of the array.
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().
这篇关于从 PHP 数组输出(回显/打印)所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!