以可读/分层格式显示数组

下面是为数组提取数据的代码

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

当我使用 print _ r ($data)时,它读作:

Array ( [0] => Array ( [0] => Natural Chlid 1 [1] => Natural Chlid 2 [2] => Natural Chlid 3 ) )

我想说的是:

自然儿童1
自然之子2
自然之子3 < br/>

282969 次浏览
foreach($array as $v) echo $v, PHP_EOL;

更新 : 一个更复杂的解决方案是:

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

这个打印出来:

    [
key1 => val1
key2 => val2
key3 => [
subkey1 => subval1
subkey2 => subval2
subkey3 => [
subsubkey1 => subsubval1
subsubkey2 => subsubval2
]
]
]

试试这个:

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

取代 print_r($data)

print_r()主要用于调试。如果希望以该格式打印,请循环遍历数组,然后将元素打印出来。

foreach($data as $d){
foreach($d as $v){
echo $v."\n";
}
}
echo '<pre>';
foreach($data as $entry){
foreach($entry as $entry2){
echo $entry2.'<br />';
}
}

Instead of

print_r($data);

试试看

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

我有一个基本功能:

function prettyPrint($a) {
echo "<pre>";
print_r($a);
echo "</pre>";
}


prettyPrint($data);

编辑: 优化函数

function prettyPrint($a) {
echo '<pre>'.print_r($a,1).'</pre>';
}

编辑: 具有自定义标签支持的 Moar 优化功能

function prettyPrint($a, $t='pre') {echo "<$t>".print_r($a,1)."</$t>";}
<?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];
}
?>

我用这个向访问者显示一个漂亮的打印数组

使用 var _ dump 函数在 php 中打印格式化数组的非常好的方法。

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

我认为 var_export()var_dump()被遗忘的兄弟拥有最好的输出——它更紧凑:

echo "<pre>";
var_export($menue);
echo "</pre>";

顺便说一下: 并非所有情况下都需要使用 <pre>。当你查看网页的源代码时,var_dump()var_export()已经被格式化了。

This may be a simpler solution:

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

对于单个数组,您可以使用内爆,它有一个更干净的结果打印。

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


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

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

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

我用它来获取键和它们的值 $qw = mysqli _ query ($connect,$query) ;

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

我会用在线工具。

如果有人需要查看数组这么酷;)使用此方法. . 这将打印到您的浏览器控制台

function console($obj)
{
$js = json_encode($obj);
print_r('<script>console.log('.$js.')</script>');
}

你可以像这样使用。

console($myObject);

输出将是这样的. . 如此凉爽啊! !

enter image description here

我假设有人使用 print _ r 进行调试。然后我建议使用像 肯特这样的库。这允许以可读的格式显示大数组:

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

enter image description here

用于快速简单的 JSON 表示的一行程序:

    echo json_encode($data, JSON_PRETTY_PRINT);

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

    echo Yaml::dump($data);

这将尝试改进控制台应用程序中的 print_r()输出格式:

function pretty_printr($array) {


$string = print_r($array, TRUE);


foreach (preg_split("/((\r?\n)|(\r\n?))/", $string) as $line) {


$trimmed_line = trim($line);


// Skip useless lines.
if (!$trimmed_line || $trimmed_line === '(' || $trimmed_line === ')' || $trimmed_line === 'Array') {
continue;
}


// Improve lines ending with empty values.
if (substr_compare($trimmed_line, '=>', -2) === 0) {
$line .=  "''";
}


print $line . PHP_EOL;
}
}

Example:

[activity_score] => 0
[allow_organisation_contact] => 1
[cover_media] => Array
[image] => Array
[url] => ''
[video] => Array
[url] => ''
[oembed_html] => ''
[thumb] => Array
[url] => ''
[created_at] => 2019-06-25T09:50:22+02:00
[description] => example description
[state] => published
[fundraiser_type] => anniversary
[end_date] => 2019-09-25
[event] => Array
[goal] => Array
[cents] => 40000
[currency] => EUR
[id] => 37798
[your_reference] => ''