如何在 PHP 中回显或打印数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9816889/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:41:24  来源:igfitidea点击:

How to echo or print an array in PHP?

phparrays

提问by EnexoOnoma

I have this array

我有这个数组

Array
(
  [data] => Array
    (
      [0] => Array
        (
          [page_id] => 204725966262837
          [type] => WEBSITE
        )

      [1] => Array
        (
          [page_id] => 163703342377960
          [type] => COMMUNITY
        )
      )
)

My question is how can I just echo the content without this structure? I tried

我的问题是如何在没有这种结构的情况下回显内容?我试过

foreach ($results as $result) {
    echo $result->type; 
    echo "<br>";
} 

采纳答案by Shiplu Mokaddim

This will do

这会做

foreach($results['data'] as $result) {
? ? echo $result['type'], '<br>';
}

回答by Ibrahim Azhar Armar

To see the contents of arrayyou can use.

查看数组的内容,您可以使用。

1)print_r($array);or if you want nicely formatted array then:

1)print_r($array);或者如果您想要格式良好的数组,则:

echo '<pre>'; print_r($array); echo '</pre>';

2)use var_dump($array)to get more information of the content in the array like datatype and length.

2)用于var_dump($array)获取数组中内容的更多信息,如数据类型和长度。

3)you can loop the array using php's foreach();and get the desired output. more info on foreach in php's documentation website:
http://in3.php.net/manual/en/control-structures.foreach.php

3)您可以使用php循环数组foreach();并获得所需的输出。有关 php 文档网站中 foreach 的更多信息:http:
//in3.php.net/manual/en/control-structures.foreach.php

回答by Mark E

If you just want to know the content without a format (e.g. for debuging purpose) I use this:

如果您只想知道没有格式的内容(例如用于调试目的),我使用这个:

echo json_encode($anArray);

This will show it as a JSON which is pretty human readable.

这将把它显示为一个人类可读的 JSON。

回答by Mohammad

There are multiple function to printing array content that each has features.

打印数组内容有多种功能,每个功能都有。

print_r()

print_r()

Prints human-readable information about a variable.

打印有关变量的人类可读信息。

$arr = ["a", "b", "c"];
echo "<pre>";
print_r($arr);
echo "</pre>";
Array
(
    [0] => a
    [1] => b
    [2] => c
)



var_dump()

var_dump()

Displays structured information about expressions that includes its type and value.

显示有关表达式的结构化信息,包括其类型和值。

echo "<pre>";
var_dump($arr);
echo "</pre>";
array(3) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
}



var_export()

var_export()

Displays structured information about the given variable that returned representation is valid PHP code.

显示有关给定变量的结构化信息,该变量返回的表示是有效的 PHP 代码。

echo "<pre>";
var_export($arr);
echo "</pre>";
array (
  0 => 'a',
  1 => 'b',
  2 => 'c',
)

Note that because browser condense multiple whitespace characters (including newlines) to a single space (answer) you need to wrap above functions in <pre></pre>to display result in correct format.

请注意,由于浏览器将多个空白字符(包括换行符)压缩为一个空格(answer),因此您需要将上述函数包裹起来<pre></pre>以正确的格式显示结果。



Also there is another way to printing array content with certain conditions.

还有另一种方法可以在某些条件下打印数组内容。

echo

echo

Output one or more strings. So if you want to print array content using echo, you need to loop through array and in loop use echoto printing array items.

输出一个或多个字符串。因此,如果要使用 打印数组内容echo,则需要遍历数组并在循环中使用echo以打印数组项。

foreach ($arr as $key=>$item){
    echo "$key => $item <br>";
}
0 => a 
1 => b 
2 => c 

回答by Ankur Tiwari

You can use print_r, var_dumpand var_exportfuncations of php:

您可以使用print_r,var_dumpvar_exportphp函数:

print_r: Convert into human readble form

print_r: 转换成人类可读的形式

<?php
echo "<pre>";
 print_r($results); 
echo "</pre>";
?>

var_dump(): will show you the type of the thing as well as what's in it.

var_dump(): 将向您展示事物的类型以及其中的内容。

var_dump($results);

foreach loop: using for each loop you can iterate each and every value of an array.

foreach loop:使用 for each 循环,您可以迭代数组的每个值。

foreach($results['data'] as $result) {
    echo $result['type'].'<br>';
}

回答by Walker

Did you try using print_rto print it in human-readable form?

您是否尝试使用print_r人类可读的形式打印它?

回答by Rezigned

foreach($results['data'] as $result) {
    echo $result['type'], '<br />';
}

or echo $results['data'][1]['type'];

或者 echo $results['data'][1]['type'];

回答by Vinod Kirte

You have no need to put for loop to see the data into the array, you can simply do in following manner

您无需使用 for 循环来查看数组中的数据,您只需按照以下方式进行

<?php
echo "<pre>";
 print_r($results); 
echo "</pre>";
?>

回答by Niclas

I know this is an old question but if you want a parseable PHP representation you could use:

我知道这是一个老问题,但如果你想要一个可解析的 PHP 表示,你可以使用:

$parseablePhpCode = var_export($yourVariable,true);

If you echo the exported code to a file.php (with a return statement) you may require it as

如果您将导出的代码回显到 file.php(带有 return 语句),您可能需要它作为

$yourVariable = require('file.php');

回答by Heider Sati

I checked the answer however, (for each) in PHP is deprecated and no longer work with the latest php versions.

但是,我检查了答案,PHP 中的(对于每个)已弃用,并且不再适用于最新的 php 版本。

Usually we would convert an array into a string to log it somewhere, perhaps debugging or test etc.

通常我们会将数组转换为字符串以将其记录在某处,可能是调试或测试等。

I would convert the array into a string by doing:

我会通过执行以下操作将数组转换为字符串:

$Output = implode(",", $SourceArray);

Whereas:

然而:

$output is the result (where the string would be generated

$output 是结果(将在其中生成字符串

",": is the separator (between each array field

",": 是分隔符(在每个数组字段之间

$SourceArray: is your source array.

$SourceArray: 是你的源数组。

I hope this helps

我希望这有帮助