Connecting Tech Pros Worldwide Forums | Help | Site Map

creating a 'print_r'-like function

lawpoop@gmail.com
Guest
 
Posts: n/a
#1: Mar 23 '07
Hello!

I am working on a map of a rather large php project that I've been
working on. I hope to create a map of the files of the project that
would look like the output of the unix 'tree' command:

..
|-- index.php
| |-- menu.php
| |-- content.php
| |-- admin_page.php
| | |-- admin_function1.php.inc
| | |-- admin_function2.php.inc
| | |-- admin_function3.php.inc
| | `-- admin_subpage.php
| | |-- admin_subfunction1.php.inc
| | |-- admin_subfunction1.php.inc
| | `-- admin_subfunction1.php.inc
| `-- user_page.php
| |-- user_function1.php.inc
| |-- user_function2.php.inc
| |-- user_function3.php.inc
| `-- user_subpage.php
| |-- user_subfunction1.php.inc
| |-- user_subfunction1.php.inc
| `-- user_subfunction1.php.inc
|
|-- orphan_page.php
`-- deprecated_page.php

I can scan my php files and pull out references to php file names.
I've created an array that looks like this:

Array
(
[0] =./index.php
[1] =Array
(
[0] =./clients.php
[1] =Array
(
[0] =./login_function.inc
[1] =./client.php
[2] =./client_new.php
[3] =./client_edit.php
)
[2] =./inventory.php
[3] =Array
(
[0] =./login_function.inc
[1] =./inventory_item_add.php
[2] =./inventory_item_remove.php
)

[4] =./daily_report.php
[5] =./weekly_report.php
[6] =./monthly_report.php
[7] =./catalog.php
[8] =Array
(
[0] =./login_function.inc
[1] =./catalog_item_add.php
[2] =./catalog_item_remove.php
)
[9] =./orders.php
[10] =Array
(
[0] =./login_function.inc
[1] =./view_order.php
[2] =./order_new.php
[3] =./order_edit.php
)
[11] =./invoices.php
[12] =Array
(
[0] =./login_function.inc
[1] =./view_invoice.php
[2] =./invoice_new.php
[3] =./invoice_edit.php
[4] =./invoice_print.php
[5] =./invoice_email.php
)
)
[2] =./clients_old.php
[3] =./old_inventory.php
)

However, I'm having problems with the function that reads the array
and outputs the tree-like results. This is what I'm getting:

- ./index.php
- - ./clients.php
- - - ./login_function.inc
- - - ./client.php
- - - ./client_new.php
- - - ./client_edit.php
- - - ./inventory.php
- - - - ./login_function.inc
- - - - ./inventory_item_add.php
- - - - ./inventory_item_remove.php
- - - - ./daily_report.php
- - - - ./weekly_report.php
- - - - ./monthly_report.php
- - - - ./catalog.php
- - - - - ./login_function.inc
- - - - - ./catalog_item_add.php
- - - - - ./catalog_item_remove.php
- - - - - ./orders.php
- - - - - - ./login_function.inc
- - - - - - ./view_order.php
- - - - - - ./order_new.php
- - - - - - ./order_edit.php
- - - - - - ./invoices.php
- - - - - - - ./login_function.inc
- - - - - - - ./view_invoice.php
- - - - - - - ./invoice_new.php
- - - - - - - ./invoice_edit.php
- - - - - - - ./invoice_print.php
- - - - - - - ./invoice_email.php
- - ./clients_old.php
- - ./old_inventory.php


And this is my code:

<?

show_array( $array );

function show_array( $array, $level = 0, $counter = 0 ) {

$count = count($array);


foreach ( $array as $key =$value ) {

if ( is_array($value) ) {

$level++;

$counter = 0;

show_array( $value, $level, $counter );

} else {

$counter++;

for ( $i = 0; $i <= $level; $i++ ) {
echo " - ";
}

echo "$value\n";

if ( $counter == $count ) {

$level--;

$counter = 0;
}
}
}
}


-------------------
As far as I can tell, the $level counter is not getting decremented
correctly . Can someone help me out?

Steve Lefevre


Rami Elomaa
Guest
 
Posts: n/a
#2: Mar 23 '07

re: creating a 'print_r'-like function


lawpoop@gmail.com kirjoitti:
Quote:
Hello!
>
I am working on a map of a rather large php project that I've been
working on. I hope to create a map of the files of the project that
would look like the output of the unix 'tree' command:
>
.
|-- index.php
| |-- menu.php
| |-- content.php
| |-- admin_page.php
| | |-- admin_function1.php.inc
| | |-- admin_function2.php.inc
| | |-- admin_function3.php.inc
| | `-- admin_subpage.php
| | |-- admin_subfunction1.php.inc
| | |-- admin_subfunction1.php.inc
| | `-- admin_subfunction1.php.inc
| `-- user_page.php
| |-- user_function1.php.inc
| |-- user_function2.php.inc
| |-- user_function3.php.inc
| `-- user_subpage.php
| |-- user_subfunction1.php.inc
| |-- user_subfunction1.php.inc
| `-- user_subfunction1.php.inc
|
|-- orphan_page.php
`-- deprecated_page.php
>
I can scan my php files and pull out references to php file names.
I've created an array that looks like this:
>
Array
(
[0] =./index.php
[1] =Array
(
[0] =./clients.php
[1] =Array
(
[0] =./login_function.inc
[1] =./client.php
[2] =./client_new.php
[3] =./client_edit.php
)
[2] =./inventory.php
[3] =Array
(
[0] =./login_function.inc
[1] =./inventory_item_add.php
[2] =./inventory_item_remove.php
)
>
[4] =./daily_report.php
[5] =./weekly_report.php
[6] =./monthly_report.php
[7] =./catalog.php
[8] =Array
(
[0] =./login_function.inc
[1] =./catalog_item_add.php
[2] =./catalog_item_remove.php
)
[9] =./orders.php
[10] =Array
(
[0] =./login_function.inc
[1] =./view_order.php
[2] =./order_new.php
[3] =./order_edit.php
)
[11] =./invoices.php
[12] =Array
(
[0] =./login_function.inc
[1] =./view_invoice.php
[2] =./invoice_new.php
[3] =./invoice_edit.php
[4] =./invoice_print.php
[5] =./invoice_email.php
)
)
[2] =./clients_old.php
[3] =./old_inventory.php
)
>
However, I'm having problems with the function that reads the array
and outputs the tree-like results. This is what I'm getting:
>
- ./index.php
- - ./clients.php
- - - ./login_function.inc
- - - ./client.php
- - - ./client_new.php
- - - ./client_edit.php
- - - ./inventory.php
- - - - ./login_function.inc
- - - - ./inventory_item_add.php
- - - - ./inventory_item_remove.php
- - - - ./daily_report.php
- - - - ./weekly_report.php
- - - - ./monthly_report.php
- - - - ./catalog.php
- - - - - ./login_function.inc
- - - - - ./catalog_item_add.php
- - - - - ./catalog_item_remove.php
- - - - - ./orders.php
- - - - - - ./login_function.inc
- - - - - - ./view_order.php
- - - - - - ./order_new.php
- - - - - - ./order_edit.php
- - - - - - ./invoices.php
- - - - - - - ./login_function.inc
- - - - - - - ./view_invoice.php
- - - - - - - ./invoice_new.php
- - - - - - - ./invoice_edit.php
- - - - - - - ./invoice_print.php
- - - - - - - ./invoice_email.php
- - ./clients_old.php
- - ./old_inventory.php
>
>
And this is my code:
>
<?
>
show_array( $array );
>
function show_array( $array, $level = 0, $counter = 0 ) {
>
$count = count($array);
>
>
foreach ( $array as $key =$value ) {
>
if ( is_array($value) ) {
>
$level++;
>
$counter = 0;
>
show_array( $value, $level, $counter );
>
} else {
>
$counter++;
>
for ( $i = 0; $i <= $level; $i++ ) {
echo " - ";
}
// a nice little function to replace
// the quite unnecessary loop:
str_repeat(' - ', $level);

Quote:
echo "$value\n";
>
if ( $counter == $count ) {
>
$level--;
$level is decremented here, but $counter won't reach $count on all
occasions, since it's only incremented if the branch is a leaf, yet
$count is the number of all nodes, not just leaves, but branches and
leaves (arrays and values). Well, I must admit that I don't understand
the functionality of $counter on the whole, as it's also set to zero if
the node is a branch, but I think the roots of your problems may have
something to do with how you handle $counter. I may be wrong just as
well, though :)
Quote:
$counter = 0;
}
}
}
}
>
>
-------------------
As far as I can tell, the $level counter is not getting decremented
correctly . Can someone help me out?
>
Steve Lefevre
>
Nobody said writing recursive functions is easy, but it's the most
commonly used solution for drawing a hierarchical tree. Usually, when
done right (after many trial and error), it's the best solution. You've
got a very good start here, all it needs is a little fine-tuning.

--
Rami.Elomaa@gmail.com
"Olemme apinoiden planeetalla."
Mike Russell
Guest
 
Posts: n/a
#3: Mar 23 '07

re: creating a 'print_r'-like function


<lawpoop@gmail.comwrote in message
news:1174682039.373093.136860@e65g2000hsc.googlegr oups.com...
Quote:
Hello!
>
I am working on a map of a rather large php project that I've been
working on. I hope to create a map of the files of the project that
would look like the output of the unix 'tree' command:
>
.
|-- index.php
| |-- menu.php
....
Consider emitting XML, and tweak (if necessary) a standard XML parser to do
the output.
--
Mike Russell
www.curvemeister.com/forum/


Henk verhoeven
Guest
 
Posts: n/a
#4: Mar 24 '07

re: creating a 'print_r'-like function


Hi,

Nice, i will soon need to make a user interface component that outputs
such a representation, so if i can help you now i can reuse the
algorithm myself later :-) .

For a start you could replace
for ( $i = 0; $i <= $level; $i++ ) {
echo " - ";
}
echo "$value\n";
by
for ( $i = 0; $i < $level; $i++ ) {
echo "| ";
}
echo "|-- $value\n";

This would still leave two problems. I will start with the easy one:
- the last leave is shown like |--, it should be like `--
To solve this you only have to check for $key to be the last one:
if ($key == count($array)-1) {
(...)

Once you have solved this it should output something like:
|-- index.php
| |-- menu.php
| |-- content.php
| |-- admin_page.php
| | |-- admin_function1.php.inc
| | |-- admin_function2.php.inc
| | |-- admin_function3.php.inc
| | `-- admin_subpage.php
| | | |-- admin_subfunction1.php.inc
| | | |-- admin_subfunction1.php.inc

As you see the last two lines include | below the `, it shoulr have been
a space there. One could say that the last branche does not take into
acount that the branche on the previous level has already ended. But how
could it know? This info is not passed when the show_array function was
called recursively. You need to pass this info about all the previous
levels. That would, at the moment the recursive call is made, be an
array holding a boolean for each level, and an empty array for the
initial show_array call. Let's call this array $track. It can be built
by adding the last boolean at the array that was passed through the
parameter just before it get passed in the recursive call.
The length of $track already tells the $level, so you can replace the
$level parameter with this array and use count($track) where you need
the $level.
Oncy you have the array, you can replace
for ( $i = 0; $i <= $level; $i++ ) {
echo "| ";
by:
forEach($track as $i =$continued) {
echo $continued ? "| " : " ";

I guess you can fill in the missing pieces and get it to work. I did not
really code it out and test it so there may be something i have not noticed.

Thank you for helping me with my component ;-).


Henk Verhoeven,
www.phpPeanuts.org.
Toby A Inkster
Guest
 
Posts: n/a
#5: Mar 24 '07

re: creating a 'print_r'-like function


lawpoop wrote:
Quote:
function show_array( $array, $level = 0, $counter = 0 ) {
>
$count = count($array);
>
>
foreach ( $array as $key =$value ) {
>
if ( is_array($value) ) {
>
$level++;
>
$counter = 0;
>
show_array( $value, $level, $counter );
>
} else {
>
$counter++;
>
for ( $i = 0; $i <= $level; $i++ ) {
echo " - ";
}
>
echo "$value\n";
>
if ( $counter == $count ) {
>
$level--;
>
$counter = 0;
}
}
}
}

You've overcomplicated things. You don't need all these counters and stuff.

function show_array ($array, $indent='')
{
foreach ($array as $value)
{
if (is_array($value))
show_array($value, $indent.' - ');
else
echo $indent.$value."\n";
}
}


--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
lawpoop@gmail.com
Guest
 
Posts: n/a
#6: Mar 24 '07

re: creating a 'print_r'-like function


On Mar 23, 6:50 pm, "Mike Russell" <RE-MOVEm...@Curvemeister.comRE-
MOVEwrote:
Quote:
>
Consider emitting XML, and tweak (if necessary) a standard XML parser to do
the output.
Hey Mike --

sounds like a wise idea. Do you have any recommendations for a parser?

Steve

Mike Russell
Guest
 
Posts: n/a
#7: Mar 24 '07

re: creating a 'print_r'-like function


<lawpoop@gmail.comwrote in message
news:1174749342.161230.84250@e1g2000hsg.googlegrou ps.com...
Quote:
On Mar 23, 6:50 pm, "Mike Russell" <RE-MOVEm...@Curvemeister.comRE-
MOVEwrote:
Quote:
>>
>Consider emitting XML, and tweak (if necessary) a standard XML parser to
>do
>the output.
>
Hey Mike --
>
sounds like a wise idea. Do you have any recommendations for a parser?
PHP has some built-in functions, and the expat library seems to be very
popular. You can turn any browser loose on an XML file and get a
hierarchical output similar to that in the original post.
--
Mike Russell
www.curvemeister.com/forum/


Closed Thread