简体   繁体   English

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

[英]How can I echo or print an array in PHP?

I have this array我有这个数组

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

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

How can I just echo the content without this structure?没有这种结构,我怎么能只回显内容?

I tried我试过了

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

To see the contents of array you can use:要查看数组的内容,您可以使用:

  1. print_r($array); or if you want nicely formatted array then:或者,如果您想要格式良好的数组,则:

     echo '<pre>'; print_r($array); echo '</pre>';
  2. Use var_dump($array) to get more information of the content in the array like the datatype and length.使用var_dump($array)获取数组中内容的更多信息,如数据类型和长度。

  3. You can loop the array using php's foreach();您可以使用 php 的foreach(); and get the desired output.并获得所需的输出。 More info on foreach is in PHP's documentation website:foreach更多关于foreach的信息在 PHP 的文档网站:foreach

This will do这会做

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

If you just want to know the content without a format (eg, for debugging purposes) I use this:如果您只想知道没有格式的内容(例如,出于调试目的),我使用这个:

echo json_encode($anArray);

This will show it as a JSON which is pretty human-readable.这将把它显示为一个非常可读的 JSON。

There are multiple functions for printing array content that each has features.有多种打印数组内容的功能,每个功能都有。

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()

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()

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 the browser condenses multiple whitespace characters (including newlines) to a single space ( answer ), you need to wrap above functions in <pre></pre> to display result in thee correct format.请注意,由于浏览器会将多个空白字符(包括换行符)压缩为一个空格( answer ),因此您需要将上述函数包装在<pre></pre>中以以正确的格式显示结果。


Also, there is another way to print array content with certain conditions.此外,还有另一种方法可以在某些条件下打印数组内容。

echo

Output one or more strings.输出一个或多个字符串。 So if you want to print array content using echo , you need to loop through the array and in the loop use echo to print array items.因此,如果要使用echo打印数组内容,则需要遍历数组并在循环中使用echo打印数组项。

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

You can use print_r , var_dump and var_export functions of PHP:您可以使用 PHP 的print_rvar_dumpvar_export函数:

print_r : Convert into human-readable 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 a 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>';
}

尝试使用print_r以人类可读的形式打印它。

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

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

You don't have any need to use a for loop to see the data into the array.您无需使用for循环来查看数组中的数据。 You can simply do it in the following manner:您可以通过以下方式简单地执行此操作:

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

人类可读的(例如,可以记录到文本文件中......):

print_r($arr_name, TRUE);

You can use var_dump() function to display structured information about variables/expressions, including its type and value, or you can use print_r() to display information about a variable in a way that's readable by humans.您可以使用var_dump()函数显示有关变量/表达式的结构化信息,包括其类型和值,或者您可以使用print_r()以人类可读的方式显示有关变量的信息。

Example: Say we have got the following array, and we want to display its contents.示例:假设我们有以下数组,并且我们想要显示它的内容。

$arr = array ('xyz', false, true, 99, array('50'));

print_r() function - Displays human-readable output print_r() 函数 - 显示人类可读的输出

Array
(
    [0] => xyz
    [1] =>
    [2] => 1
    [3] => 99
    [4] => Array
        (
            [0] => 50
        )
)

var_dump() function - Displays values and types var_dump() 函数 - 显示值和类型

array(5) {
  [0]=>
  string(3) "xyz"
  [1]=>
  bool(false)
  [2]=>
  bool(true)
  [3]=>
  int(100)
  [4]=>
  array(1) {
    [0]=>
    string(2) "50"
  }
}

The functions used in this answer can be found on the PHP documentation website, var_dump() and print_r() .此答案中使用的函数可以在 PHP 文档网站var_dump()print_r()上找到。

For more details:更多细节:

If you want a parsable 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');

I checked the answers, however, (for each) in PHP it is deprecated and no longer works with the latest PHP versions.我检查了答案,但是(对于每个)在 PHP 中它已被弃用,并且不再适用于最新的 PHP 版本。

Usually, we would convert an array into a string to log it somewhere, perhaps debugging, 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: 是您的源数组。

If you only need echo 'type' field, you can use function ' array_column ' like:如果您只需要 echo 'type' 字段,则可以使用函数 ' array_column ',如:

$arr = $your_array;
echo var_dump(array_column($arr['data'], 'type'));

Loop through and print all the values of an associative array, You could use a foreach loop, like this:循环并打印关联数组的所有值,您可以使用foreach循环,如下所示:

foreach($results as $x => $value) {
    echo $value;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM