Skip to content

Instantly share code, notes, and snippets.

@asika32764
Last active August 5, 2022 01:55
Show Gist options
  • Select an option

  • Save asika32764/7339081 to your computer and use it in GitHub Desktop.

Select an option

Save asika32764/7339081 to your computer and use it in GitHub Desktop.
Useful recursive dumping function to simulate `print_r()` but limited by level.
<?php
/**
* @copyright Copyright (C) 2011 - 2014 SMS Taiwan, Inc. All rights reserved.
* @license GNU General Public License version 2 or later;
*/
/**
* Recursive print variables and limit by level.
*
* @param mixed $data The variable you want to dump.
* @param int $level The level number to limit recursive loop.
*
* @return string Dumped data.
*/
function printRLevel($data, $level = 5)
{
static $innerLevel = 1;
static $tabLevel = 1;
$self = __FUNCTION__;
$type = gettype($data);
$tabs = str_repeat(' ', $tabLevel);
$quoteTabes = str_repeat(' ', $tabLevel - 1);
$output = '';
$elements = array();
$recursiveType = array('object', 'array');
// Recursive
if (in_array($type, $recursiveType))
{
// If type is object, try to get properties by Reflection.
if ($type == 'object')
{
$output = get_class($data) . ' ' . ucfirst($type);
$ref = new \ReflectionObject($data);
$properties = $ref->getProperties();
foreach ($properties as $property)
{
$property->setAccessible(true);
$pType = $property->getName();
if ($property->isProtected())
{
$pType .= ":protected";
}
elseif ($property->isPrivate())
{
$pType .= ":" . $property->class . ":private";
}
if ($property->isStatic())
{
$pType .= ":static";
}
$elements[$pType] = $property->getValue($data);
}
}
// If type is array, just retun it's value.
elseif ($type == 'array')
{
$output = ucfirst($type);
$elements = $data;
}
// Start dumping data
if ($level == 0 || $innerLevel < $level)
{
// Start recursive print
$output .= "\n{$quoteTabes}(";
foreach ($elements as $key => $element)
{
$output .= "\n{$tabs}[{$key}] => ";
// Increment level
$tabLevel = $tabLevel + 2;
$innerLevel++;
$output .= in_array(gettype($element), $recursiveType) ? $self($element, $level) : $element;
// Decrement level
$tabLevel = $tabLevel - 2;
$innerLevel--;
}
$output .= "\n{$quoteTabes})\n";
}
else
{
$output .= "\n{$quoteTabes}*MAX LEVEL*\n";
}
}
else
{
$output = $data;
}
return $output;
}
<?php
/**
* Dump Array or Object as tree node. If you send multiple params in this method,
* this function will batch print it.
*
* If last param is integer, it will set to be level limited.
* So make sure your last param is not integer if you want to print it.
*
* Usage:
*
* ``` php
* show($array, $object, $anyVariable);
*
* show($array, $objecy, 15);
* ```
*
* @param mixed $data Array or Object to dump.
* @param int $level The level number to limit recursive loop.
*
* @since 1.0
*
* @return void
*/
function show($data)
{
$args = func_get_args();
$last = array_pop($args);
if (is_int($last))
{
$level = $last;
}
else
{
$level = 4;
$args[] = $last;
}
// Dump Multiple values
if (count($args) > 1)
{
$prints = array();
$i = 1;
foreach ($args as $arg)
{
$prints[] = "[Value " . $i . "]\n" . printRLevel($arg, $level);
$i++;
}
echo '<pre>' . implode("\n\n", $prints) . '</pre>';
}
else
{
// Dump one value.
echo '<pre>' . printRLevel($data, $level) . '</pre>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment