creating a 'print_r'-like function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lawpoop@gmail.com

    creating a 'print_r'-like function

    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.p hp
    | | |-- admin_subfuncti on1.php.inc
    | | |-- admin_subfuncti on1.php.inc
    | | `-- admin_subfuncti on1.php.inc
    | `-- user_page.php
    | |-- user_function1. php.inc
    | |-- user_function2. php.inc
    | |-- user_function3. php.inc
    | `-- user_subpage.ph p
    | |-- user_subfunctio n1.php.inc
    | |-- user_subfunctio n1.php.inc
    | `-- user_subfunctio n1.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.ph p
    [5] =./weekly_report.p hp
    [6] =./monthly_report. php
    [7] =./catalog.php
    [8] =Array
    (
    [0] =./login_function. inc
    [1] =./catalog_item_ad d.php
    [2] =./catalog_item_re move.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.ph p
    [2] =./invoice_new.php
    [3] =./invoice_edit.ph p
    [4] =./invoice_print.p hp
    [5] =./invoice_email.p hp
    )
    )
    [2] =./clients_old.php
    [3] =./old_inventory.p hp
    )

    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.ph p
    - - - - ./weekly_report.p hp
    - - - - ./monthly_report. php
    - - - - ./catalog.php
    - - - - - ./login_function. inc
    - - - - - ./catalog_item_ad d.php
    - - - - - ./catalog_item_re move.php
    - - - - - ./orders.php
    - - - - - - ./login_function. inc
    - - - - - - ./view_order.php
    - - - - - - ./order_new.php
    - - - - - - ./order_edit.php
    - - - - - - ./invoices.php
    - - - - - - - ./login_function. inc
    - - - - - - - ./view_invoice.ph p
    - - - - - - - ./invoice_new.php
    - - - - - - - ./invoice_edit.ph p
    - - - - - - - ./invoice_print.p hp
    - - - - - - - ./invoice_email.p hp
    - - ./clients_old.php
    - - ./old_inventory.p hp


    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

    #2
    Re: creating a 'print_r'-like function

    lawpoop@gmail.c om kirjoitti:
    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.p hp
    | | |-- admin_subfuncti on1.php.inc
    | | |-- admin_subfuncti on1.php.inc
    | | `-- admin_subfuncti on1.php.inc
    | `-- user_page.php
    | |-- user_function1. php.inc
    | |-- user_function2. php.inc
    | |-- user_function3. php.inc
    | `-- user_subpage.ph p
    | |-- user_subfunctio n1.php.inc
    | |-- user_subfunctio n1.php.inc
    | `-- user_subfunctio n1.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.ph p
    [5] =./weekly_report.p hp
    [6] =./monthly_report. php
    [7] =./catalog.php
    [8] =Array
    (
    [0] =./login_function. inc
    [1] =./catalog_item_ad d.php
    [2] =./catalog_item_re move.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.ph p
    [2] =./invoice_new.php
    [3] =./invoice_edit.ph p
    [4] =./invoice_print.p hp
    [5] =./invoice_email.p hp
    )
    )
    [2] =./clients_old.php
    [3] =./old_inventory.p hp
    )
    >
    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.ph p
    - - - - ./weekly_report.p hp
    - - - - ./monthly_report. php
    - - - - ./catalog.php
    - - - - - ./login_function. inc
    - - - - - ./catalog_item_ad d.php
    - - - - - ./catalog_item_re move.php
    - - - - - ./orders.php
    - - - - - - ./login_function. inc
    - - - - - - ./view_order.php
    - - - - - - ./order_new.php
    - - - - - - ./order_edit.php
    - - - - - - ./invoices.php
    - - - - - - - ./login_function. inc
    - - - - - - - ./view_invoice.ph p
    - - - - - - - ./invoice_new.php
    - - - - - - - ./invoice_edit.ph p
    - - - - - - - ./invoice_print.p hp
    - - - - - - - ./invoice_email.p hp
    - - ./clients_old.php
    - - ./old_inventory.p hp
    >
    >
    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);

    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 :)
    $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@gma il.com
    "Olemme apinoiden planeetalla."

    Comment

    • Mike Russell

      #3
      Re: creating a 'print_r'-like function

      <lawpoop@gmail. comwrote in message
      news:1174682039 .373093.136860@ e65g2000hsc.goo glegroups.com.. .
      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



      Comment

      • Henk verhoeven

        #4
        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.p hp
        | | | |-- admin_subfuncti on1.php.inc
        | | | |-- admin_subfuncti on1.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.

        Comment

        • Toby A Inkster

          #5
          Re: creating a 'print_r'-like function

          lawpoop wrote:
          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($valu e))
          show_array($val ue, $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!

          Comment

          • lawpoop@gmail.com

            #6
            Re: creating a 'print_r'-like function

            On Mar 23, 6:50 pm, "Mike Russell" <RE-MOVEm...@Curvem eister.comRE-
            MOVEwrote:
            >
            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

            Comment

            • Mike Russell

              #7
              Re: creating a 'print_r'-like function

              <lawpoop@gmail. comwrote in message
              news:1174749342 .161230.84250@e 1g2000hsg.googl egroups.com...
              On Mar 23, 6:50 pm, "Mike Russell" <RE-MOVEm...@Curvem eister.comRE-
              MOVEwrote:
              >>
              >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



              Comment

              Working...