SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    May 2001
    Posts
    193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Creating array from recursive function

    Im using this recursive function to display a menu tree, which it does fine. (you'll have to forgive the asterisks in the code, i dont know why they are showing up here)

    PHP Code:
    function display_tree($parent$level

    * *
    $result mysql_query('SELECT title FROM tree WHERE parent="'.$parent.'";'); 
    * *
        while (
    $row mysql_fetch_array($result)) 
        { 
            echo 
    str_repeat('  ',$level).$row['title']."\n"
        * * 
            
    display_tree($row['title'], $level+1); 
        } 

    But instead of printing it I want to place it in an array that would contain items indexed by the items parent. I tried this and various other versions of this to no avail. I think the whole recursion thing is confusing me a bit and overwriting items in my array.
    PHP Code:
    function display_tree($parent$level

    * *
    $result mysql_query('SELECT title FROM tree WHERE parent="'.$parent.'";'); 
    * *
        while (
    $row mysql_fetch_array($result)) 
        { 
            
    $my_array[$parent][] =  str_repeat('  ',$level).$row['title']."\n"
        * * 
            
    display_tree($row['title'], $level+1); 
        } 
        
        return 
    $my_array

    So if my tree prints like this:
    PHP Code:
    Section 1
      Link 1
      Link 2
    Section 
      Link 1
      Link 2
    Loose link 
    I want to produce an array like this:
    PHP Code:
    Array
    {
        [
    0] =>
            {
            [
    0] => Loose link
            
    }
        
        [
    Section 1] =>
            {
            [
    0] => Link 1
            
    [1] => Link 2
            
    }
            
        [
    Section 2] =>
            {
            [
    0] => Link 1
            
    [1] => Link 2
            
    }



  2. #2
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    I think making $my_array static may do the trick.

    There's an example of using a static variable in a recursive function here:
    http://www.php.net/manual/en/languag...bles.scope.php

    see if this works:

    PHP Code:
    <?php
    function display_tree($parent$level
    {   
       static 
    $my_array;

       
    $result mysql_query('SELECT title FROM tree WHERE parent="'.$parent.'";'); 
     
        while (
    $row mysql_fetch_array($result)) 
        { 
            
    $my_array[$parent][] =  str_repeat('  ',$level).$row['title']."\n"

            
    display_tree($row['title'], $level+1); 
        } 
         
        return 
    $my_array 
    }   


    ?>

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Recursive functions that query everytime is a pretty big, memory hog, you should be able to pull it off with just one query and and use PHP to create the array.

    See my post here
    http://www.sitepointforums.com/showt...ght=menusystem


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
  •