如何在PHP中响应数组?

时间:2022-08-27 17:46:39

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>";
} 

11 个解决方案

#1


69  

This will do

这将做

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

#2


308  

To see the contents of array you can use.

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

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

1)print_r(数组)美元;或者如果你想要格式良好的数组:

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

#3


66  

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,非常适合人类阅读。

#4


16  

I normally use for debugging:

我通常用于调试:

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

This will result in an easy readable array dump.

这将导致一个容易读懂的数组转储。

#5


12  

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

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

或echo $结果(“数据”)[1]['类型'];

#6


11  

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

您尝试过使用print_r以人类可读的形式打印它吗?

#7


9  

<?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);

The foreach loop

foreach循环

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

#8


4  

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>";
?>

#9


3  

You can use var_dump($array);

您可以使用var_dump(数组)美元;

It dumps all information about the variable and is very useful.

它转储关于变量的所有信息,非常有用。

#10


0  

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

如果您将导出的代码回显到一个文件中。php(带有返回语句)可能需要它as

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

#11


0  

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;
}

#1


69  

This will do

这将做

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

#2


308  

To see the contents of array you can use.

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

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

1)print_r(数组)美元;或者如果你想要格式良好的数组:

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

#3


66  

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,非常适合人类阅读。

#4


16  

I normally use for debugging:

我通常用于调试:

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

This will result in an easy readable array dump.

这将导致一个容易读懂的数组转储。

#5


12  

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

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

或echo $结果(“数据”)[1]['类型'];

#6


11  

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

您尝试过使用print_r以人类可读的形式打印它吗?

#7


9  

<?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);

The foreach loop

foreach循环

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

#8


4  

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>";
?>

#9


3  

You can use var_dump($array);

您可以使用var_dump(数组)美元;

It dumps all information about the variable and is very useful.

它转储关于变量的所有信息,非常有用。

#10


0  

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

如果您将导出的代码回显到一个文件中。php(带有返回语句)可能需要它as

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

#11


0  

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;
}