Witam wszystkich!
Jak wiadomo często zachodzi konieczność sprawdzenia zawartości tablicy, typu zmiennej etc.
Print_r pokazuje w sposób przyjemny dla oka, ale bez typów zmiennych, z kolei var_dump ma wszystko czego trzeba, ale jak na mój gust jest niezbyt czytelny.
Z tego też względu, na własne potrzeby zmajstrowałem prostą funkcję wyświetlającą dane w sposób równie czytelny jak print_r, ale z określeniem typów zmiennych jak var_dump.
Różnicę stanowi podejście do zmiennych typu object i resource - w tej funkcji wyświetlany jest jedynie odpowiedni typ oraz nazwa klasy w przypadku obiektu i typ "zasobu" w przypadku resource (np mysql link)
Przykład:
<?php
'12',
4,
new FrontController,
$this->System->Smarty,
'item1' => 'item1value',
'subitem1' => '55',
'subitem2' => false,
'subitem3' => 4,
'subarrayitem1' => 'subarrayitem1value',
2.4,
'subarrayitem2'
),
null
)
);
showArray($tab1, 'Informacja dodatkowa');
?>
Efekt:

Jeżeli ktoś uzna to za przydatne, poniżej kod (jeśli komuś nudziłoby się na tyle, żeby wtrącić swoje uwagi to będę wdzięczny

)
<?php
function showArray($input, $additionalText='', $inputKey='', $level=0, $start=true) {
if($start) {
echo '<div style="text-align:left;font-size:9pt;color:#454545;width:auto;margin:10px auto;padding:10px 20px;"> <p style="text-align:left;font-weight:bold;color:#A60000;text-decoration:underline;margin:5px 0;">
'.$additionalText.'</p>';
}
case 'array':
$type = 'Array('.sizeof($input).')<br />{'; $inputText = '';
break;
case 'string':
$type = 'String('.strlen($input).')'; break;
case 'integer':
$type = 'Integer';
$inputText = $input;
break;
case 'boolean':
$type = 'Boolean';
break;
case 'double':
$type = 'Float';
$inputText = $input;
break;
case 'object':
$type = 'Object of';
$inputText = get_class($input);
break;
case 'resource':
$type = 'Resource of';
$inputText = get_resource_type($input);
break;
case 'NULL':
$type = 'NULL';
$inputText = '';
break;
}
$string = "<div style=\"margin:0 0 0 %dpx\">%s<span style=\"font-size:7pt;color:#009900;\">%s</span>
<span style=\"font-weight:bold;font-size:9pt;color:#000000;\">%s</span></div>";
$arrayClosing = "<div style=\"margin:15px 0 0 %dpx\">}</div>";
$inputKey = (is_string($inputKey)) ?
'["<strong>'.$inputKey.'</strong>"] => ' : '[<strong>'.$inputKey.'</strong>] => ' ;
printf($string, 0, '', $type, $inputText); return;
}
printf($string, $level*50
, ($start) ?
'' : $inputKey, $type, $inputText); foreach($input AS $key=>$value) {
showArray($value, $additionalText, $key, $level+1, false);
}
printf($arrayClosing, $level*50
); } else {
printf($string, $level*50
, $inputKey, $type, $inputText); }
if($start) {
}
}
?>
Ten post edytował drPayton 30.10.2007, 23:04:16