从 PHP 数组输出(回显/打印)所有内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/693637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Output (echo/print) everything from a PHP Array
提问by nickcharlton
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.
不指定“id”而是输出数组的完整内容。
回答by Bj?rn
If you want to format the output on your own, simply add another loop (foreach) to iterate through the contents of the current row:
如果你想自己格式化输出,只需添加另一个循环(foreach)来遍历当前行的内容:
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.
或者,如果您不关心格式,请使用前面答案中推荐的 print_r 函数。
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().
print_r() 仅打印数组的键和值,与 var_dump() 相对,后者还打印数组中数据的类型,即 String、int、double 等。如果您确实关心数据类型 - 使用 var_dump() 而不是 print_r()。
回答by karim79
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.
上述功能将保留原始格式,使其在网络浏览器中(更)可读。
回答by Anonymous
var_dump()can do this.
var_dump()可以做到这一点。
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.
此函数显示有关一个或多个表达式的结构化信息,包括其类型和值。数组和对象通过缩进以显示结构的值进行递归探索。
回答by SoapBox
回答by Laurence
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.
如果您正在调试数组,这是我一直使用的一个小函数,它很方便。它几乎与 Darryl 和 Karim 发布的内容相同。我刚刚添加了一个参数标题,所以你有一些调试信息作为你正在打印的数组。它还会检查您是否提供了一个有效的数组,如果没有,它会通知您。
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
||---------------------------------||
回答by brian-brazil
You can use print_rto get human-readable output.
您可以使用print_r来获取人类可读的输出。
回答by Sudhamsh Kandukuri
You can use print_r to get human-readable output. But to display it as text we add "echo '';"
您可以使用 print_r 获得人类可读的输出。但是要将其显示为文本,我们添加“echo '';”
echo ''; print_r($row);
回声''; 打印_r($row);
回答by Darryl Hein
Similar to karim's, but with print_r which has a much small output and I find is usually all you need:
类似于 karim 的,但 print_r 的输出很小,我发现通常是你所需要的:
function PrintR($var) {
echo '<pre>';
print_r($var);
echo '</pre>';
}
回答by Viren Patel
//@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
每次需要字符串或数组时都使用此函数,它会很好用。
有 2 个参数
1.$data - 它可以是数组或字符串
2.$d - 默认情况下它是 FALSE 但如果你设置为 true 那么它将执行 die() 函数
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.
}

