For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > May 2007 > print_r with perline (nice) formatting for arrays - How?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author print_r with perline (nice) formatting for arrays - How?
cluthz

2007-05-24, 6:59 pm

I've lived with print_r printing my array variable output on a single
wrapped line for some time now.

But I now want to know how to make print_r print an array on multiple lines
making it easier to read.

I have tried to google this but difficult to know what keywords to use and
thus have not found good results.

Does anyone know how I can do this?

Thanks in advance.


Joe Scylla

2007-05-24, 6:59 pm

cluthz wrote:
> I've lived with print_r printing my array variable output on a single
> wrapped line for some time now.
>
> But I now want to know how to make print_r print an array on multiple lines
> making it easier to read.
>
> I have tried to google this but difficult to know what keywords to use and
> thus have not found good results.
>
> Does anyone know how I can do this?
>
> Thanks in advance.
>
>


i'm using that function for printing out variables, arrays or objects.
hope that helps.

/**
* Dumps the values of an variable, array or object.
*
* @param mixed $v
* @return void
*/
function dump($v)
{
echo "\n<pre>\n" . "- dump " . str_repeat("-", 79 - strlen("- dump "))
.. "\n";
if(is_array($v))
{
print_r($v);
}
elseif (is_object($v))
{
print_r($v);
}
else
{
var_dump($v);
}
echo str_repeat("-", 79) . "\n</pre>\n";
}
mark AT markduncanDOTnet

2007-05-25, 6:58 pm

foreach($array as $key => $value)
{
echo $key . ' = ' . $value;
echo "<br>";
}
peter

2007-05-25, 6:58 pm


> foreach($array as $key => $value)
> {
> echo $key . ' = ' . $value;
> echo "<br>";
> }


That is ok for a 1 dimensional array but multi dimensional arrays will have
problems.

You can simply use the <pre></pre> tage to ensure that the output is as
output in text.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com