Results 1 to 6 of 6

Thread: Stumped with simple recursive function

  1. #1
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default Stumped with simple recursive function

    Given:
    Code:
    function table($array) {
      foreach ($array as $x) {
        if (is_array($x)) {
          table($x);
        } else echo "<div class=\"table_cell\">$x</div>\n";
      } echo "<div class=\"clear\"></div>\n";
    }
    
    table(
      array(
        array("1", "2"),
        array("3", "4")
      )
    );
    I don't understand why it produces:
    Code:
    <div class="table_cell">1</div>
    <div class="table_cell">2</div>
    <div class="clear"></div>
    <div class="table_cell">3</div>
    <div class="table_cell">4</div>
    <div class="clear"></div>
    <div class="clear"></div>
    I've tried to run through this recursive function mentally but I just can't seem to understand why it produces an extra div.

    What I'm trying to do, is write:
    Code:
    <div class="clear"></div>
    at the end of each sub-array.

    Any thoughts anyone?
    - Mike

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by mburt View Post
    Given:
    PHP Code:
    function table($array) {
      foreach (
    $array as $x) {
        if (
    is_array($x)) {
          
    table($x);
        } else echo 
    "<div class=\"table_cell\">$x</div>\n";
      } echo 
    "<div class=\"clear\"></div>\n";
    }

    table( array( array("1""2"), array("3""4") ); 
    I've tried to run through this recursive function mentally but I just can't seem to understand why it produces an extra div.
    you are passing three arrays to the function; so logically, div.clear is being output three times.

    Code:
    <div class="table_cell">1</div>  <!--$array[0][0]-->
    <div class="table_cell">2</div>  <!--$array[0][1]-->
    <div class="clear"></div>        <!--$array[0] (after each child string is processed)-->
    <div class="table_cell">3</div>  <!--$array[1][0]-->
    <div class="table_cell">4</div>  <!--$array[1][1]-->
    <div class="clear"></div>        <!--$array[1] (after each child string is processed)-->
    <div class="clear"></div>        <!--$array (after each child array is processed)-->

  3. The Following User Says Thank You to traq For This Useful Post:

    mburt (09-25-2011)

  4. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Any suggestions traq on how I might achieve what I'm looking for? I'm too tired to think, personally... lol
    - Mike

  5. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    PHP Code:
    <?php
    function table($array) {
      foreach (
    $array as $x) {
        if (
    is_array($x)) {
          
    table($x);
          echo 
    "<div class=\"clear\"></div>\n";
        } else {
            echo 
    "<div class=\"table_cell\">$x</div>\n";
        }
      }
    }

    table(
      array(
        array(
    "1""2"),
        array(
    "3""4")
      )
    );
    Jeremy | jfein.net

  6. The Following User Says Thank You to Nile For This Useful Post:

    mburt (09-25-2011)

  7. #5
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Hm, I never thought you could output anything after the table() function is called inside the loop. Works like a charm!
    - Mike

  8. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    No Problem! Yeah it's interesting to play around with using recursive functions.
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •