Skip to content

Instantly share code, notes, and snippets.

@insideone
Created May 15, 2016 13:33
Show Gist options
  • Save insideone/11e06a7c3c6f5db9de5599e7a8d36fd6 to your computer and use it in GitHub Desktop.
Save insideone/11e06a7c3c6f5db9de5599e7a8d36fd6 to your computer and use it in GitHub Desktop.
PHP: вывод массива в виде таблицы (рекурсивно)
<?php
function pt($data, $openTree = true, $maxLevel = 0, $path = array())
{
$level = count($path);
static $id = 0;
if ( $level == 0 )
$id++;
?>
<? if ( $id == 1 ) : ?>
<style>
table.dbgt {
border-collapse: collapse;
}
table.dbgt td {
vertical-align: top;
border: 1px solid black;
padding: 4px;
}
table.dbgt a.show {
text-decoration: none;
border-bottom: 1px dashed black;
}
</style>
<script type="text/javascript">
function __pt_scroll_result(a)
{
var show = a.innerHTML == 'show';
a.nextElementSibling.style.display = show ? 'block' : 'none';
a.innerHTML = show ? 'hide' : 'show';
return false;
}
</script>
<? endif; ?>
<? if ( $level > 0 && ! $openTree ) : ?>
<a href="#" class="show" onclick="return __pt_scroll_result(this);" title="<?=htmlspecialchars(implode(' / ', $path))?>">show</a>
<? endif; ?>
<table class="dbgt" id="dbgt-<?=$id?>" <?=$level == 0 || $openTree ? '' : 'style="display: none;"'?>>
<?
if ( is_array($data) )
{
foreach($data as $key => $value)
{
?>
<tr>
<td><?=htmlspecialchars($key)?></td>
<td>
<?
$isArrayVal = is_array($value);
if ( $isArrayVal && count($value) > 0 )
{
if ( $maxLevel == 0 || $maxLevel > $level )
{
call_user_func(__FUNCTION__, $value, $openTree, $maxLevel, array_merge($path, array($key)));
}
else
{
echo '...';
}
}
else
{
echo $isArrayVal ? 'array()' : htmlspecialchars(var_export($value, true));
}
?>
</td>
</tr>
<?}
}
else
{?>
<tr>
<td colpan="2"><?=htmlspecialchars($data)?></td>
</tr>
<?}
?>
</table>
<?}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment