PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Aug 19, 2018 7:19 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Tue Feb 10, 2004 1:19 pm 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6445
Location: 53.01N x 112.48W
Here's a little function I wrote because I found the default print_r was useless when viewing large, nested arrays. Enjoy!

Syntax: [ Download ] [ Hide ]
function ordered_print_r($p_array,$current_key = '')

{

  $current_key = ($current_key != '')? "$current_key => " : '';



  echo <<<TABLE

<table>

  <tr>

    <td>

      $current_key<strong>Array(</strong>

    </td>

  </tr>

TABLE
;



  while($row = each($p_array))

  {

    echo <<<ELEMENT

  <tr>

    <td>

      &nbsp;//put a couple non-breaking spaces here

      &nbsp;//put a couple non-breaking spaces here

    </td>

    <td>

ELEMENT
;

    if(gettype($row[value]) == "array")

    {

      echo "<td><td>".ordered_print_r($row[value],$row[key])."</td>";

    }

    else

    {

      echo  <<<ROW

    <td>

      $row[key]

    </td>

    <td>

      =>$row[value]

    </td>

ROW
;

    }

echo <<<ELEMENT

  </tr>

ELEMENT
;

  }

  echo <<<TABLEEND

  <tr>

    <th align = "right">

      )

    </th>

  </tr>

</table>

TABLEEND
;

}


Obvously there can be some formatting that can be done to taste.

Here's an example of the output

Syntax: [ Download ] [ Hide ]
$array[0] = "orange";

$array[1] = array("yellow"=>"banana","red"=>"apple","green"=>"another apple");



ordered_print_r($array);


Will return:

Syntax: [ Download ] [ Hide ]
=> Array(  

                0 =>orange

                1 => Array(  

                                  yellow =>banana

                                  red =>apple

                                  green =>another apple

                                )  

   

            )

Let me know if you use this folks, or if you can find room for improvement. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 4:13 am 
Offline
Admin
User avatar

Joined: Wed Aug 13, 2003 7:02 am
Posts: 4522
Location: York, UK
hi mate,

Don't wanna p*ss on your bonfire, but you could have just done this

Syntax: [ Download ] [ Hide ]
echo "<pre>";

print_r($array);

echo "</pre>";


Gives you nearly the same result.

Mark


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 4:15 am 
Offline
DevNet Resident

Joined: Tue Nov 18, 2003 2:09 pm
Posts: 1826
Location: Middlesbrough, UK
lololol :lol:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 9:35 am 
Offline
DevNet Master

Joined: Thu Jan 30, 2003 9:26 pm
Posts: 2893
Location: Glasgow, Scotland
It was a good idea since it can be hard to assimilate a big, unformatted array.

I use the snippet Bech mentioned as a utility fn - will work on objects as well as arrays.

Syntax: [ Download ] [ Hide ]
<?php

function printR($array)

{

    echo '<pre>';

    print_r($array);

    echo '</pre>';

}

?>


Last edited by McGruff on Tue Aug 09, 2005 6:41 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 10:44 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6445
Location: 53.01N x 112.48W
Bech100 wrote:
Don't wanna p*ss on your bonfire, but you could have just done this

Syntax: [ Download ] [ Hide ]
echo "<pre>";
print_r($array);
echo "</pre>";



. . . . . . . . . . . . . .oh.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 10:50 am 
Offline
Her Royal Site Adminness
User avatar

Joined: Tue Apr 23, 2002 2:21 am
Posts: 5371
Location: Essex, UK
While we're sharing, this is my array debugging function - I added the ability to pass a bit of text (normally for me it's the array name), the line number and the file that the function is being called from (I always pass __LINE__ and __FILE__). Just makes it easier for me to debug stuff:
Syntax: [ Download ] [ Hide ]
function view_array($array, $text='', $line=0, $file='')

{

        $text = (!empty($text)) ? '<u>'.$text.'</u> -> ' : '';

       

        echo '<pre>';

        echo (!empty($file)) ?  '<b>File:</b> '.$file.'<br />' : '';

        echo (!empty($line)) ?  '<b>Line:</b> '.$line.'<br />' : '';

        echo $text;

        print_r($array);

        echo '</pre>';



} // end func

Mac


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 11:05 am 
Offline
Admin
User avatar

Joined: Wed Aug 13, 2003 7:02 am
Posts: 4522
Location: York, UK
i already posted an Array Debugger here http://forums.devnetwork.net/viewtopic. ... highlight=

Might wanna merge that with this thread.

Mark


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 15, 2004 2:42 pm 
Offline
DevNet Resident
User avatar

Joined: Fri Aug 08, 2003 6:53 pm
Posts: 2101
Location: Sweden
I use twigletmac's version, but have added is_array(), is_int() etc. checks, so that I can debug whatever without needing to know what it is I'm sending along...

Just because that I "know" that I'm trying to use an array, doesn't mean that I really am, and because I often wants to debug-print other vars.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group