Welcome Guest, Not a member yet? Register   Sign In
Little help with recursive function plz
#1

[eluser]Christophe28[/eluser]
Hi,

I'm trying to write a recursive function where the results ($item->id and $item->name) are returned in a multidimensional array:

This is how far I got in the category_model:

Code:
function get_childnodes($id) {
    
        // Create an array to store the childnodes
        $childnodes = array();
        
        // Select id, name and type of the deleted categories
        $q = "SELECT id, name, type FROM categories WHERE parent_id = ?";
        
        // Execute the query
        $sql = $this->db->query($q, array($id));
        
        // If there is a result ...
        if ( $sql->num_rows() > 0 ) {
        
            $results = $sql->result();
            
            foreach ( $results as $item ) {
                
                // If it is a category, search for more categories
                if ( $item->type == 0 ) {
                    
                    $this->category_model->get_childnodes($item->id);
                // echo $item->id . ' ';
                // echo $item->name . ' ';
                }
                
            }
        
        }
        
        // Return an array of ids
        return $childnodes;
        
    }
When you comment out the two echo statements, you get a nice line with the id's and names of all the childnodes (recursively, so also the children from the children, and so on ...)

The result I would be returned in the controller as follow:

Code:
$result_array = $this->category_model->get_childnodes($id);

print_r($result_array);

// RESULT
array(
    '0' => array(
            '0' => 'id_2',
            '1' => 'name'
            ),
    '1' => array(
            '0' => 'id_3',
            '1', 'other name'
            )
);

I hope somebody can help me out with this one!

Thanks in advance!

Christophe
#2

[eluser]marjune[/eluser]
hi! you have to make two arrays in it.

Code:
$childnodes  = array(); //in side in this array would be another array for the name and id.

$id_and_name = array(); // array of your name and id.
$counter     = 0;// counter of the childnodes  array.
$idcounter   = 0;//counter for id_and_name inside the childnodes array

foreach ( $results as $item ) {
                
                // If it is a category, search for more categories
                if ( $item->type == 0 ) {
                    
                    $this->category_model->get_childnodes($item->id);
                // echo $item->id . ' ';
                // echo $item->name . ' ';
                   $id_and_name[$idcounter]   = $item->id;
                   $id_and_name[$idcounter+1] = $item->name ;
                }
                $idcounter++;
       }
      
              // Return an array of ids
        return $childnodes[$counter] = $id_and_name;
#3

[eluser]Jaketoolson[/eluser]
Quote: $id_and_name[$idcounter] = $item->id;
$id_and_name[$idcounter+1] = $item->name ;

this would create the following array:

Code:
array(
    '0' => array(
            '0' => 'id'
            ),
    '1' => array(
            '0' => 'name'
            )
);

Just do this
Code:
$i = 0;
foreach ( $results as $item )
{
  
  $childnodes[$i]['id']   = $item->id;
  $childnodes[$i]['name'] = $item->name;

  $i++;              
}

//creates
array(
    '0' => array(
            id => 'id_2',
            name => 'name'
            ),
    '1' => array(
            id => 'id_3',
            name, 'other name'
            )
);
#4

[eluser]marjune[/eluser]
$id_and_name[$idcounter] = $item->id;
$id_and_name[$idcounter+1] = $item->name ;

nope it doesn't return to because i often doing this stuff

Code:
array(
    '0' => array(
            '0' => 'id'
            ),
    '1' => array(
            '0' => 'name'
            )

);
as long as the $counter would increment after initiating the code below
Code:
return $childnodes[$counter] = $id_and_name;

@Jaketoolson: but the best is yours! Big Grin
#5

[eluser]Christophe28[/eluser]
Hi,

I'm familiar on how to create a multidimensional array in a foreach, but how to create a multidimensional array in a recursive function? A recursive function is a function which basically calls itself, so you have to pass the array somehow to create one big multidim array with all the values and return it on the end ...

Hope somebody can take a look.

Christophe
#6

[eluser]marjune[/eluser]
what i think about is, u need another function to call that initiate the multi arrays inside your recursion!!
#7

[eluser]Christophe28[/eluser]
Hi,

The problem is already solved. Thanks for all the input!

I'll post a link when the article (about this topic) is done!

Christophe
#8

[eluser]InsiteFX[/eluser]
Your problem was because you were defining the array in your function, so everytime you re-entered it it resets the array!

InsiteFX
#9

[eluser]Christophe28[/eluser]
Exactly, that was part of the problem. I was also using the wrong function (array_push, instead of array_merge) Smile




Theme © iAndrew 2016 - Forum software by © MyBB