以可读/层次化格式显示数组

时间:2021-04-13 13:31:36

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。

17 个解决方案

#1


29  

Try this:

试试这个:

foreach($data[0] as $child) {
   echo $child . "\n";
}

in place of print_r($data)

代替print_r(元数据)

#2


364  

Instead of

而不是

print_r($data);

try

试一试

print "<pre>";
print_r($data);
print "</pre>";

#3


49  

print("<pre>".print_r($data,true)."</pre>");

#4


41  

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

#5


11  

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.

顺便说一句:使用

并非总是必要的。查看网页的源代码时,已经对var_dump()和var_export()进行了格式化。

#6


5  

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 !!

输出是这样的。真酷啊! !

以可读/层次化格式显示数组

#7


3  

foreach($array as $v) echo $v, PHP_EOL;

#8


3  

This may be a simpler solution:

这可能是一个更简单的解决方案:

echo implode('<br>', $data[0]);

#9


2  

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

#10


2  

I assume one uses print_r for debugging. I would then suggest using libraries like Kint. This allows displaying big arrays in a readable format:

我假设其中一个使用print_r进行调试。然后我建议使用Kint这样的库。这允许以可读的格式显示大数组:

$data = [['Natural Child 1', 'Natural Child 2', 'Natural Child 3']];
Kint::dump($data, $_SERVER);

以可读/层次化格式显示数组

#11


1  

Very nice way to print formatted array in php, using the var_dump function.

使用var_dump函数在php中打印格式化数组的好方法。

 $a = array(1, 2, array("a", "b", "c"));
 var_dump($a);

#12


1  

I use this for getting keys and their values $qw = mysqli_query($connection, $query);

我使用它来获取键及其值$qw = mysqli_query($connection, $query);

while ( $ou = mysqli_fetch_array($qw) )
{
    foreach ($ou as $key => $value) 
    {
            echo $key." - ".$value."";
    }
    echo "<br/>";
}

#13


1  

I would just use online tools.

我会使用在线工具。

#14


1  

One-liner for a quick-and-easy JSON representation:

简单快捷的JSON表示:

    echo json_encode($data, JSON_PRETTY_PRINT);

If using composer for the project already, require symfony/yaml and:

如果已经在项目中使用composer的话,需要symfony/yaml和:

    echo Yaml::dump($data);

#15


0  

echo '<pre>';
foreach($data as $entry){
    foreach($entry as $entry2){
        echo $entry2.'<br />';
    }
}

#16


0  

<?php 
//Make an array readable as string
function array_read($array, $seperator = ', ', $ending = ' and '){
      $opt = count($array);
      return $opt > 1 ? implode($seperator,array_slice($array,0,$opt-1)).$ending.end($array) : $array[0];
}
?>

I use this to show a pretty printed array to my visitors

我用这个来给我的访问者展示一个漂亮的打印数组。

#17


0  

For single arrays you can use implode, it has a cleaner result to print.

对于单个数组,您可以使用内爆,它可以打印更清晰的结果。

<?php
$msg = array('msg1','msg2','msg3');
echo implode('<br />',$msg);
echo '<br />----------------------<br/>';

echo nl2br(implode("\n",$msg));
echo '<br />----------------------<br/>';
?>

For multidimensional arrays you need to combine with some sort of loop.

对于多维数组,需要结合某种循环。

<?php
$msgs[] = $msg;
$msgs[] = $msg;
$msgs[] = $msg;
$msgs[] = $msg;
$msgs[] = $msg;
foreach($msgs as $msg) {
    echo implode('<br />',$msg);
    echo '<br />----------------------<br/>';
}
?>

#1


29  

Try this:

试试这个:

foreach($data[0] as $child) {
   echo $child . "\n";
}

in place of print_r($data)

代替print_r(元数据)

#2


364  

Instead of

而不是

print_r($data);

try

试一试

print "<pre>";
print_r($data);
print "</pre>";

#3


49  

print("<pre>".print_r($data,true)."</pre>");

#4


41  

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

#5


11  

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.

顺便说一句:使用

并非总是必要的。查看网页的源代码时,已经对var_dump()和var_export()进行了格式化。

#6


5  

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 !!

输出是这样的。真酷啊! !

以可读/层次化格式显示数组

#7


3  

foreach($array as $v) echo $v, PHP_EOL;

#8


3  

This may be a simpler solution:

这可能是一个更简单的解决方案:

echo implode('<br>', $data[0]);

#9


2  

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

#10


2  

I assume one uses print_r for debugging. I would then suggest using libraries like Kint. This allows displaying big arrays in a readable format:

我假设其中一个使用print_r进行调试。然后我建议使用Kint这样的库。这允许以可读的格式显示大数组:

$data = [['Natural Child 1', 'Natural Child 2', 'Natural Child 3']];
Kint::dump($data, $_SERVER);

以可读/层次化格式显示数组

#11


1  

Very nice way to print formatted array in php, using the var_dump function.

使用var_dump函数在php中打印格式化数组的好方法。

 $a = array(1, 2, array("a", "b", "c"));
 var_dump($a);

#12


1  

I use this for getting keys and their values $qw = mysqli_query($connection, $query);

我使用它来获取键及其值$qw = mysqli_query($connection, $query);

while ( $ou = mysqli_fetch_array($qw) )
{
    foreach ($ou as $key => $value) 
    {
            echo $key." - ".$value."";
    }
    echo "<br/>";
}

#13


1  

I would just use online tools.

我会使用在线工具。

#14


1  

One-liner for a quick-and-easy JSON representation:

简单快捷的JSON表示:

    echo json_encode($data, JSON_PRETTY_PRINT);

If using composer for the project already, require symfony/yaml and:

如果已经在项目中使用composer的话,需要symfony/yaml和:

    echo Yaml::dump($data);

#15


0  

echo '<pre>';
foreach($data as $entry){
    foreach($entry as $entry2){
        echo $entry2.'<br />';
    }
}

#16


0  

<?php 
//Make an array readable as string
function array_read($array, $seperator = ', ', $ending = ' and '){
      $opt = count($array);
      return $opt > 1 ? implode($seperator,array_slice($array,0,$opt-1)).$ending.end($array) : $array[0];
}
?>

I use this to show a pretty printed array to my visitors

我用这个来给我的访问者展示一个漂亮的打印数组。

#17


0  

For single arrays you can use implode, it has a cleaner result to print.

对于单个数组,您可以使用内爆,它可以打印更清晰的结果。

<?php
$msg = array('msg1','msg2','msg3');
echo implode('<br />',$msg);
echo '<br />----------------------<br/>';

echo nl2br(implode("\n",$msg));
echo '<br />----------------------<br/>';
?>

For multidimensional arrays you need to combine with some sort of loop.

对于多维数组,需要结合某种循环。

<?php
$msgs[] = $msg;
$msgs[] = $msg;
$msgs[] = $msg;
$msgs[] = $msg;
$msgs[] = $msg;
foreach($msgs as $msg) {
    echo implode('<br />',$msg);
    echo '<br />----------------------<br/>';
}
?>