zermy
zermy

Reputation: 621

Issues with PHP array iteration logic

The logic required in this case might be simple or not, but I couldn't figure it out. So, I am asking for help.

After a query, I get an array that looks a bit like this:

Array=>
     [0]=>
         ['name'] = item1
         ['id'] = 1
         ['parent_id'] = 0
     [1]=>
         ['name'] = item2
         ['id'] = 2
         ['parent_id'] = 1
     [2]=>
         ['name'] = item3
         ['id'] = 3
         ['parent_id'] = 5

Now, I need to make paths for each and every item in this list. The paths would look something like: /item1 for item1 and something like /item1/item2 for item 2.

Note: The items are not necessarily in order. A parent item might come after it's child...

So, basically, I need a loop (probably more than 1), that when it encounters an item, it writes down the item name preceded by a slash. Then, it looks at the parent_id and writes down the parent_id's name preceded by a slash.

Then it looks at the parent's parent_id and writes down that name preceded by a slash. It continues to do this until it encounters a parent_id of 0. At which point, it assigns a value to an array, so something like paths['item2'] = "/item1/item2" and moves on to the next id and repeats!

Thanks for all your help, have a good day!

edit: Fixed the id of item3, all the items are meant to have different id's. I was asked to improve the question: The final output array should look a bit like this:

Array=>
     ["item1"]="/item1"
     ["item2"]="/item1/item2"
     ["item3"]="/item5/item3"    

The final output would be a html select form with each item as an option, and I need to have it's path associated somehow, either with a hidden field or just through Ajax or something.

edit: I fixed the problem. I just thought I'd write out the solution here in case someone else stumbles across this. Note: Still, not exactly sure how it works, but it works! It might be inefficient, I don't know.

function getCollPath($proj_list, $length){
$total_path = "";
$paths = array();
for ($j = 0; $j < $length + 1; $j++){
    if (isset($proj_list[$j])){
        $id = $j;
        $name = $proj_list[$j]['name'];
        $total_path = getItemPath($proj_list, $id, NULL);
        $paths[$name] = $total_path;
    }
}
return $paths;
}     

function getItemPath($proj_list, $current_id, $path){

$current_parent_id = $proj_list[$current_id]['parent_id'];
$current_name = $proj_list[$current_id]['name'];
$current_path = "/".$current_name;
if ($current_parent_id == 0){
      if (isset($path)){
        return $current_path.$path;
      }
      else{
        return $current_path;
      }
}
else{
    if (!isset($path)){
        $path = $current_path;
    }
    return getItemPath($proj_list, $current_parent_id, $path);
}
}

Upvotes: 1

Views: 154

Answers (1)

Alfabravo
Alfabravo

Reputation: 7589

A recursive function. It looks out for array element with id = child.parent_id. Then it calls self with the current parent_id as parameter until a element with parent_id = "" or "0" is reached. It should return a segment of the breadcrumb to the parent call, so the original call gets the whole route

Upvotes: 1

Related Questions