0

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'
]
7
  • 1
    Can you give an example array and desired output. So it can be more understandable what you want to do Commented Aug 12, 2016 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 Commented Aug 12, 2016 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)? Commented Aug 12, 2016 at 6:26
  • From where are you calling this function getUserUbigeoString() Commented Aug 12, 2016 at 6:27
  • sorry, added the database structure, hopefully it helps Commented Aug 12, 2016 at 6:36

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, exactly what i needed. You're right, it would be better to pass the array as a parameter.
0

Use Can do it with lists method in laravel

Ex :

$ubigeoRepository->lists('ubigeo_id','id')->all();

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.