从 PHP 数组输出(回显/打印)所有内容 [英] Output (echo/print) everything from a PHP Array

查看:57
本文介绍了从 PHP 数组输出(回显/打印)所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在不指定数组的哪一部分的情况下回显或打印数组的全部内容?

场景:我试图回应以下所有内容:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆