For Programmers: Free Programming Magazines  


Home > Archive > PHP Programming > March 2007 > building an array of files









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 building an array of files
lawpoop@gmail.com

2007-03-26, 6:59 pm

Hello folks -

I'm working on a function to map a php project that's gotten a little
unwieldly. I'd like to have a reference of all files like the output
of the unix tree command, like this:
..
|-- 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 have a function that can create such text output ( or close enough )
from an array structure 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] => ./wly_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, now I'm having a problem building the array of files!

Starting with a default root file, index.php, I can loop through each
file and find each .php or .inc file. Then I can create an array like

Array (
[0] => ./index.php
[1] => Array (
[0] => ./clients.php
[2] => ./inventory.php
[4] => ./daily_report.php
[5] => ./wly_report.php
[6] => ./monthly_report.php
[7] => ./catalog.php
[9] => ./orders.php
[11] => ./invoices.php
[2] => ./clients_old.php
[3] => ./old_inventory.php
)

Then, I need to go through each element and find out what files are
referenced in that file, and insert that array. So I would get:

Array (
[0] => ./index.php
[1] => Array (
[0] => ./clients.php
[1] => Array (
[0] => ./login_function.inc
[1] => ./client.php

However, I'm having problems getting started. What function can I use
to insert an array at a certain point?

ZeldorBlat

2007-03-26, 6:59 pm

On Mar 26, 3:48 pm, lawp...@gmail.com wrote:
> Hello folks -
>
> I'm working on a function to map a php project that's gotten a little
> unwieldly. I'd like to have a reference of all files like the output
> of the unix tree command, like this:
> .
> |-- 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 have a function that can create such text output ( or close enough )
> from an array structure 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] => ./wly_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, now I'm having a problem building the array of files!
>
> Starting with a default root file, index.php, I can loop through each
> file and find each .php or .inc file. Then I can create an array like
>
> Array (
> [0] => ./index.php
> [1] => Array (
> [0] => ./clients.php
> [2] => ./inventory.php
> [4] => ./daily_report.php
> [5] => ./wly_report.php
> [6] => ./monthly_report.php
> [7] => ./catalog.php
> [9] => ./orders.php
> [11] => ./invoices.php
> [2] => ./clients_old.php
> [3] => ./old_inventory.php
> )
>
> Then, I need to go through each element and find out what files are
> referenced in that file, and insert that array. So I would get:
>
> Array (
> [0] => ./index.php
> [1] => Array (
> [0] => ./clients.php
> [1] => Array (
> [0] => ./login_function.inc
> [1] => ./client.php
>
> However, I'm having problems getting started. What function can I use
> to insert an array at a certain point?


There isn't really a straightforward way to insert an element at some
arbitrary position in an array. You can split the array in two and
then stitch it back together with something in the middle.

Instead of processing your files level by level, why not do it
recursively? For instance, when your parse index.php, instead of
adding clients.php, then inventory.php, and then going back and
checking for their includes, add clients.php and check for his
includes. When that's done, add inventory.php and check for his
includes. That way you never need to insert the new array in the
middle of the parent -- it'll just happen in the right order the first
time.

CptDondo

2007-03-26, 6:59 pm

ZeldorBlat wrote:

> Instead of processing your files level by level, why not do it
> recursively? For instance, when your parse index.php, instead of
> adding clients.php, then inventory.php, and then going back and
> checking for their includes, add clients.php and check for his
> includes. When that's done, add inventory.php and check for his
> includes. That way you never need to insert the new array in the
> middle of the parent -- it'll just happen in the right order the first
> time.
>


Or store the info as XML and use XPath.... The first bit of work would
be harder, but then all the lookups and stuff would be there for you.

And you really don't have to care where the stuff is in the tree - just
look it up every time and add child nodes.

--Yan
Sponsored Links







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

Copyright 2008 codecomments.com