Jump to content

Recommended Posts

My tree gather function gives me multidimensional array. However i want it to be one dimensional . I was getting one dimensional earlier however i forgot how i got it. Can someone please tell me my mistake

 

Code

<?php
include'config.php';
function tree_gather($node)   
  {
     
$sql = "SELECT lchild,rchild FROM tree WHERE parent = '$node'";
    $execsql = mysql_query($sql);
    $array = mysql_fetch_array($execsql);
    
    if(!empty($array['lchild']) || !empty($array['rchild']))
    {
   
      $child[] = $array['lchild'];
      $child[] = $array['rchild'];
      $eh[] = tree_gather($array['lchild']);
      $child = array_merge($child, $eh);
      $eh1[] = tree_gather($array['rchild']);  
      $child =   array_merge($child, $eh1);
     
    } 
     return $child;
  }
$pair = tree_gather(1000000);
echo "<pre>";
print_r($pair);
echo "</pre>";  
?>  

 

Output

Array
(
    [0] => 3632873
    [1] => 5951538
    [2] => Array
        (
            [0] => 8930480
            [1] => 7563232
            [2] => Array
                (
                    [0] => 1144744
                    [1] => 4386810
                    [2] => 
                    [3] => 
                )

            [3] => 
        )

    [3] => 
)

Did so with same multidimensional array. I believe the below code needs changes to make it onedimensional

$eh[] = tree_gather($array['lchild']);
      $child = array_merge($child, $eh);
      $eh1[] = tree_gather($array['rchild']);  
      $child =   array_merge($child, $eh1);

 

Its due to $eh[] and $eh1[]. Get rid of the square brackets.

 

The following should do it.

$lchild = tree_gather($array['lchild']);
$rchild = tree_gather($array['rchild']);
$child = array_merge($lchild, $rchild);

Its giving me warnings.

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\Users\.php on line 17

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in C:xxx\xxx\xxx.php on line 17

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\xxx\xxx\xxx.php line 17

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in C:\xxx\xxx\xxx.php on line 17

Warning: array_merge() [function.array-merge]: Argument #1 is not an array in C:\xxx\xxx\xxx.php on line 17

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in C:\xxx\xxx\xxx.php on line 17

 

Line 17 being $child = array_merge($lchild, $rchild);

Recursion always has a base case and a recursive case.  You need to be clear on what to do for each one.

 

1.  Base case - the tree node has no children.  What action to take?  Is the correct action to return an empty array?

2.  Recursive case - the tree node has a left child.  What action to take?  Process left child, keep results in an array, and ..

3.  Recursive case - the tree node has a right child.  What action to take?  Process right child, keep results in an array, and combine with any results from recursive case 2.  Then return combined results.

 

Now the questions I have for you:

 

1.  What does your code do if the node has no children?  What value does it return?  It should be an empty array, written "array()", which is different from an uninitialized variable.  PHP won't like it if you try to merge an uninitialized variable with an array.

2.  What does your code do if the left child exists but the right child doesn't?  It should process just the left child if only the left child exists.  Same with the right child.

 

The problem with the arrays being inside arrays is because you used $eh[] instead of $eh, and $eh1[] instead of $eh1.  wildteen is correct there, and adding the brackets will only give the wrong results, it doesn't actually fix the problem.  The actual problem was that your function does not always return an array, it sometimes returns an unintialized $child.

Thank you for the explanation btherl. I made a new function now. I added levels to the ID too . However my function wont retrieve the last entry in the table having no rchild and have just lchild. Function attached with the image of the table. My function wont add '9567182' to the array.

<?php
function tree_gather($node, $level)   
  {
     
$sql = "SELECT lchild,rchild FROM tree WHERE parent = '$node'";
    $execsql = mysql_query($sql);
    $array = mysql_fetch_array($execsql);
    
    if(!empty($array['lchild']) || !empty($array['rchild']))
    {
      if(!empty($array['lchild']))
      $child[] = $array['lchild']."_".$level;
      if(!empty($array['rchild']))
      $child[] = $array['rchild']."_".$level;
      if(!empty($array['lchild']))
      $lchild = tree_gather($array['lchild'], $level + 1);
      if(!empty($array['rchild']))
      $rchild = tree_gather($array['rchild'], $level + 1);
      if(!empty($lchild) && !empty($rchild)) {
      $merge_both = array_merge($lchild, $rchild);
      $child = array_merge($child, $merge_both);    
      }
      elseif(!empty($lchild) && empty($rchild))
      $child = array_merge($child, $lchild);
      elseif(!empty($rchild) && empty($lchild))
      $child = array_merge($child, $rchild); 
     
    } 
     return $child;
  } ?>

 

Output

Array
(
    [0] => 3632873_1
    [1] => 5951538_1
    [2] => 8930480_2
    [3] => 7563232_2
    [4] => 1144744_3
    [5] => 4386810_3
    [6] => 1236598_2
    [7] => 2305948_2
    [8] => 9523654_3
    [9] => 5692147_3
)

 

 

 

Table Image

tree_dat.PNG

 

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.