SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Tree Simulation. Stumped

    Hello. I'm simulating a tree using the GD library. The tree will have 2 branches on each node. I've come to a problem. I'm using arrays within arrays within arrays of coordinates. I want to insert the new arrays into the very last set of arrays.

    I can *get* to the last arrays using array_walk_recursive and do all the necessary calculations but i can't retrieve the path ($v[0][1][2]) to insert the new arrays.

    This is what my array will look like roughly. An array with 2 arrays in with 2 arrays in each of those etc.

    Code:
    Array
    (
        [0] => a
        [1] => b
        [2] => Array
            (
                [0] => c
                [1] => d
                [2] => Array
                    (
                        [0] => g
                        [1] => h
                    )
    
                [3] => Array
                    (
                        [0] => i
                        [1] => j
                    )
    
            )
    
        [3] => Array
            (
                [0] => e
                [1] => f
                [2] => Array
                    (
                        [0] => k
                        [1] => l
                    )
    
                [3] => Array
                    (
                        [0] => m
                        [1] => n
                    )
    
            )
    
    )

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you really need to produce a path? Or do you just need a way to work with the endpoint?

    You could walk the array recursively, and when you find the endpoint, your function could return that array by reference. Or you could have your recursive walking routine carry a value to insert as it traverses the structure, and when it reaches the endpoint, it inserts the value it carries.

    Have you considered maybe using an object oriented structure kinda like the DOM? In my opinion, its well suited for this. If you're familiar with it(maybe from javascript) you might be very productive using it. php's existing DOM extension could be used, or you could make your own simpler classes.

  3. #3
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could walk the array recursively, and when you find the endpoint, your function could return that array by reference.
    Returning the arrays are ok, it's just adding a new element to the end arrays.

    Or you could have your recursive walking routine carry a value to insert as it traverses the structure, and when it reaches the endpoint, it inserts the value it carries.
    The values are calculated based on the previous properties. My problem is that I can't insert these properties.

    And no I've never used DOM or coded much in javascript.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It would help if you did make this using objects, but it may be a severe performance drop (especially memory usage) because PHP isn't compiled. But you wouldn't need to use DOM. I'm not sure why you would want to use DOM.

    Maybe you should re-organize your array:
    Code:
    {
        coord => [X, Y],
        branches => {
            {
                coord => [X2, Y2],
                branches => {
                    ...
                }
            },
            {
                coord => [X3, Y3],
                branches => {
                }
            },
        }
    }

  5. #5
    SitePoint Member
    Join Date
    Jun 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand why that format would change anything.

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sheep92342 View Post
    Returning the arrays are ok, it's just adding a new element to the end arrays.
    That's why I suggested you return it by reference.
    PHP Code:
    function & get_node_by_coordinates(&$tree$coord0$coord1) {
        if (
    $tree[0] === $coord0 && $tree[1] === $coord1) {
            return 
    $tree;
        }
        if (isset(
    $tree[3])) {
            
    $node =& get_node_by_coordinates($tree[3], $coord0$coord1);
            if (
    $node) {
                return 
    $node;
            }
        }
        if (isset(
    $tree[4])) {
            
    $node =& get_node_by_coordinates($tree[4], $coord0$coord1);
            if (
    $node) {
                return 
    $node;
            }
        }
        return 
    false;

    PHP Code:
    $mn_node =& get_node_by_coordinates($tree'm''n');
    print_r($mn_node);
    $mn_node[3] = 'hi!'// add a new array(node) here
    print_r($tree); 


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
  •