Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

I'm trying to create a recursive method to fill an array with each new item found.

function getUserUbigeoString($ubigeo_id){
    $ubigeoRepository = new \App\Repositories\UbigeoRepository();
    $ubigeos = array();

    $ubigeo = $ubigeoRepository->getUbigeo($ubigeo_id);

    if(!empty($ubigeo)){
        if($ubigeo->ubigeo_id == null){
            $ubigeos[] = $ubigeo->name;
        }

        $ubigeos[] = getUserUbigeoString($ubigeo->ubigeo_id);
    }

    return $ubigeos;
}

The objective of the code is to get an array fill with all the name of the ubigeos.

0 => ubigeo1
1 => ubigeo2
2 => ubigeo3
etc...

As of right now, i have tried placing the return many different locations, but the closest result i have gotten was:

array:1 [▼
  0 => array:1 [▼
    0 => array:2 [▼
      0 => "Port Dusty"
      1 => []
    ]
  ]
]

==========EDIT============ Structure of database ubigeos:

id  name     level ubigeo_id
----------------------------
3    ubigeo1  1     null
37   ubigeo2  2     3
55   ubigeo3  3     37

the output would be a simple array like so, which then i could implode into a comma separated string:

array:1 [
  0 => 'ubigeo1'
  1 => 'ubigeo2'
  2 => 'ubigeo3'
]
share|improve this question
1  
Can you give an example array and desired output. So it can be more understandable what you want to do – Aman Rawat Aug 12 at 6:07
    
yes, i updates the answer with an example, its a simple array with the values, so i can use implode later on to get a comma separated string, thanks – Carlos Aug 12 at 6:22
    
Could you please add a description how the input is structured? I.e. do you just have a chain of Ubigeo entities where each ubigeo can have 0 or 1 reference to child Ubigeo (and the ubigeo.ubigeo_id defines this child entity)? – ejuhjav Aug 12 at 6:26
    
From where are you calling this function getUserUbigeoString() – Aman Rawat Aug 12 at 6:27
    
sorry, added the database structure, hopefully it helps – Carlos Aug 12 at 6:36
up vote 1 down vote accepted

so assuming that you really want to call this with function with an Ubigeo instance and only get the names from that and from the parent Ubigeo instances (i.e. calling the function with id 55 initially to get the result array), you can try something like this (I didn't want to modify your function call parameters - normally I would include the array as a function parameter instead of instantiating new one in each recursion step):

function getUserUbigeoString($ubigeo_id)
{
    $ubigeoRepository = new \App\Repositories\UbigeoRepository();
    $ubigeos = array();

    $ubigeo = $ubigeoRepository->getUbigeo($ubigeo_id);

    if(!empty($ubigeo))
    {
        if($ubigeo->ubigeo_id != null) {
            $ubigeos = getUserUbigeoString($ubigeo->ubigeo_id);
        }
        $ubigeos[] = $ubigeo->name;
    }

    return $ubigeos;
}
share|improve this answer
    
Thanks, exactly what i needed. You're right, it would be better to pass the array as a parameter. – Carlos Aug 13 at 5:36

Use Can do it with lists method in laravel

Ex :

$ubigeoRepository->lists('ubigeo_id','id')->all();
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.