dongpou1935 2011-12-15 22:11
浏览 22

PHP数组迭代逻辑的问题

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);
}
}
  • 写回答

1条回答 默认 最新

  • dongwei6700 2011-12-15 22:15
    关注

    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

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程