php 以可读/分层格式显示数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5393085/
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
Display an array in a readable/hierarchical format
提问by Xavier
Here is the code for pulling the data for my array
这是为我的数组提取数据的代码
<?php
$link = mysqli_connect('localhost', 'root', '', 'mutli_page_form');
$query = "SELECT * FROM wills_children WHERE will=73";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
if($row = mysqli_fetch_assoc($result)) {
$data = unserialize($row['children']);
}
/* free result set */
mysqli_free_result($result);
}
?>
When I use print_r($data) it reads as:
当我使用 print_r($data) 时,它读取为:
Array ( [0] => Array ( [0] => Natural Chlid 1 [1] => Natural Chlid 2 [2] => Natural Chlid 3 ) )
I would like it to read as:
我希望它读作:
Natural Child 1
Natural Child 2
Natural Child 3
自然子1
自然子2
自然子3
采纳答案by Brian Driscoll
Try this:
尝试这个:
foreach($data[0] as $child) {
echo $child . "\n";
}
in place of print_r($data)
代替 print_r($data)
回答by Phenex
Instead of
代替
print_r($data);
try
尝试
print "<pre>";
print_r($data);
print "</pre>";
回答by Shankar ARUL - jupyterdata.com
print("<pre>".print_r($data,true)."</pre>");
回答by ditto
I have a basic function:
我有一个基本功能:
function prettyPrint($a) {
echo "<pre>";
print_r($a);
echo "</pre>";
}
prettyPrint($data);
EDIT:Optimised function
编辑:优化功能
function prettyPrint($a) {
echo '<pre>'.print_r($a,1).'</pre>';
}
EDIT:Moar Optimised function with custom tag support
编辑:具有自定义标签支持的 Moar 优化功能
function prettyPrint($a, $t='pre') {echo "<$t>".print_r($a,1)."</$t>";}
回答by Hexodus
I think that var_export()
, the forgotten brother of var_dump()
has the best output - it's more compact:
我认为var_export()
,被遗忘的兄弟var_dump()
拥有最好的输出——它更紧凑:
echo "<pre>";
var_export($menue);
echo "</pre>";
By the way: it's not allway necessary to use <pre>
. var_dump()
and var_export()
are already formatted when you take a look in the source code of your webpage.
顺便说一句:并非总是需要使用<pre>
. var_dump()
并且var_export()
在您查看网页的源代码时已经格式化。
回答by shakee93
if someone needs to view arrays so cool ;) use this method.. this will print to your browser console
如果有人需要如此酷的查看数组;) 使用此方法.. 这将打印到您的浏览器控制台
function console($obj)
{
$js = json_encode($obj);
print_r('<script>console.log('.$js.')</script>');
}
you can use like this..
你可以这样使用..
console($myObject);
Output will be like this.. so cool eh !!
输出将是这样的..太酷了啊!!
回答by Beta Projects
This may be a simpler solution:
这可能是一个更简单的解决方案:
echo implode('<br>', $data[0]);
回答by Rocket Hazmat
print_r()
is mostly for debugging. If you want to print it in that format, loop through the array, and print the elements out.
print_r()
主要用于调试。如果你想以这种格式打印它,循环遍历数组,然后打印出元素。
foreach($data as $d){
foreach($d as $v){
echo $v."\n";
}
}
回答by Yaronius
foreach($array as $v) echo $v, PHP_EOL;
UPDATE: A more sophisticated solution would be:
更新:更复杂的解决方案是:
$test = [
'key1' => 'val1',
'key2' => 'val2',
'key3' => [
'subkey1' => 'subval1',
'subkey2' => 'subval2',
'subkey3' => [
'subsubkey1' => 'subsubval1',
'subsubkey2' => 'subsubval2',
],
],
];
function printArray($arr, $pad = 0, $padStr = "\t") {
$outerPad = $pad;
$innerPad = $pad + 1;
$out = '[' . PHP_EOL;
foreach ($arr as $k => $v) {
if (is_array($v)) {
$out .= str_repeat($padStr, $innerPad) . $k . ' => ' . printArray($v, $innerPad) . PHP_EOL;
} else {
$out .= str_repeat($padStr, $innerPad) . $k . ' => ' . $v;
$out .= PHP_EOL;
}
}
$out .= str_repeat($padStr, $outerPad) . ']';
return $out;
}
echo printArray($test);
This prints out:
这打印出来:
[
key1 => val1
key2 => val2
key3 => [
subkey1 => subval1
subkey2 => subval2
subkey3 => [
subsubkey1 => subsubval1
subsubkey2 => subsubval2
]
]
]