/    Sign up×
Community /Pin to ProfileBookmark

Array Display Function

While in development mode on my site, I often used to resort to print_r to view the contents of a given array. There were a couple set backs about this function that I did not like.

First, it prints everything on the page in a single lined string. The user needs to go to View Source to view the array structure easily.

Second, it prints it at the position in the script it was called. Since, I return my html into a variable and then print the varaible at the end of the script, it always displays the printed array at the top of the screen.

Therefore, I put together a very simple and straightforward function to display array contents. I was going to go post it over at php.net, but their user contributed notes do not seem to be functioning. So, I figured I would post it here for now and get feedback from you guys, till php.net is working.

Any thoughts on how to improve it?

[code=php]
<?php
function showarr($array){
$show=”<div style=’margin:5px; font:normal 10px Courier,Arial; text-align:left; ‘>==========<br />n”;

function sharr($key,$input,$num){
$px=$num*15;
$retarr=”<div style=’margin-left:{$px}px; ‘>[{$key}] => ‘{$input}'</div>n”;
if(is_array($input)){
$num++;
foreach($input AS $skey=>$sval){
$retarr.=sharr($skey,$sval,$num);
}
$num–;
}
return $retarr;
}

$num=0;
foreach($array AS $key=>$val){
$show.=sharr($key,$val,$num);
}

$show.=”==========</div>n”;
return $show;
}
?>[/code]

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@themartyJun 28.2006 — for me this works better

[code=php]function showArray($array)
{
echo "<pre>n";var_dump($array);echo "</pre>n";
}[/code]


as it gives more information
Copy linkTweet thisAlerts:
@ShmohelauthorJun 28.2006 — Seems for my use it woulf have some of the same issues I have with print_r. But, to each his own.
×

Success!

Help @Shmohel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...