0

I want to create an array using recursion in Codeigniter. my function make_tree() in controller is :

function make_tree($customer_id,$arr = array()){

    $ctree = $this->customer_operations->view_customer_tree($customer_id);

    foreach($ctree as $v):
        echo $customer_id = $v['customer_id'];

        array_push($arr, $customer_id);

        $this->make_tree($customer_id);
    endforeach;

    var_dump($arr);

}

But the var_dump($arr) and echo results output like:

1013

array
  empty

array
  0 => string '13' (length=2)

11

array
  empty

array
  0 => string '10' (length=2)
  1 => string '11' (length=2)

How can I make a single array of all the three outputs, ie an array with elements 13,10,11

3
  • 1
    I would say try passing the array to your function by reference instead of by value, this way it's working on the same array instead of creating a new one each recurse. basically initialize the array outside of the function then change the parameter '$arr = array()' to '&$arr' Commented Oct 9, 2013 at 5:12
  • Thanks .. It helped. Though its outputs lots of arrays, I getting my desired output in the last array. Commented Oct 9, 2013 at 5:17
  • it outputs the same array growing as you're dumping it on every function run. And you're welcome, glad it worked! remove the var dump in the function and just dump the original array after the function call. Commented Oct 9, 2013 at 5:20

2 Answers 2

1

you need to send the array with the parameters otherwise a new array is created.

function make_tree($customer_id,$arr = array()){

    $ctree = $this->customer_operations->view_customer_tree($customer_id);

    foreach($ctree as $v):
        echo $customer_id = $v['customer_id'];

        array_push($arr, $customer_id);

        $this->make_tree($customer_id, $arr);
    endforeach;

    var_dump($arr);

}

PS: I don't know what you are trying to do exactly, but you probably need to add a stopping condition that is going to return the final array, unless you want to pass it by reference.

UPDATE

Here is one way to do it:

function make_tree($customer_id, &$arr)
{
    $ctree = $this->customer_operations->view_customer_tree($customer_id);

    foreach($ctree as $v):
        $customer_id = $v['customer_id'];

        array_push($arr, $customer_id);

        $this->make_tree($customer_id, $arr);
    endforeach;
}

and this is how you'd use it:

$final_array = array();
make_tree($some_customer_id, $final_array);
// now the $final_array is populated with the tree data
Sign up to request clarification or add additional context in comments.

1 Comment

If I pass the array by reference &$arr = array(), it outputs ( or returns because I am going to return the array ) an array for every $customer_id. How can I generate a single array with all the $customer_id in it? Thanks
0

You can use class scope.

class TheController {

private $arr = array();

function make_tree($customer_id){

    $ctree = $this->customer_operations->view_customer_tree($customer_id);

    foreach($ctree as $v) {
        $customer_id = $v['customer_id'];

        array_push($this->arr, $customer_id);

        $this->make_tree($customer_id);
    }

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.