【问题标题】:Get path from adjacency list data从邻接表数据中获取路径
【发布时间】:2015-06-25 12:07:39
【问题描述】:

我有一个数组(来自邻接表的数据),它看起来像:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Anniversary
            [parent] => 0
        )

    [1] => Array
        (
            [id] => 12
            [name] => New arrives
            [parent] => 1
        )

    [2] => Array
        (
            [id] => 13
            [name] => Discount
            [parent] => 12
        )

    [3] => Array
        (
            [id] => 6
            [name] => Birthday
            [parent] => 0
        )
)

我正在寻找通过 ID 检索路径的方法;

For example: getPath(13): Anniversary->New arrives->Discount;
For example: getPath(12): Anniversary->New arrives;
For example: getPath(1): Anniversary;
For example: getPath(6): Birthday;

我该怎么做? 谢谢!

【问题讨论】:

  • 你已经尝试过什么

标签: php arrays adjacency-list


【解决方案1】:
function getpath($id, $arr, $level = 0) {
    $result = array();
    foreach($arr as $key => $value){
        if($id == $value['id']){
            $result[] = $value['name'];
            $id = $value['parent'];
            if($id != 0){
              $result = array_merge($result, getpath($id, $arr, $level+1));
            }else{
                break;
            }
        }
    }
    return $level ? $result : implode('->',array_reverse($result));
}
echo getpath(13,$arr);

【讨论】:

  • 你写$key =>的目的是什么?你不使用它
  • 没用的..@splash58
  • @splash58 我知道我正在努力,你有解决方案吗
  • $result[$key] = $value['name'];然后 echo implode('->',$result);
  • 你终于..谢谢队友@splash58
【解决方案2】:

考虑这个数组,

$input = [
    ['id'=>1, 'name'=>'Anniversary', 'parent'=>0],
    ['id'=>12, 'name'=>'New arrives', 'parent'=>1],
    ['id'=>13, 'name'=>'Discount', 'parent'=>12],
    ['id'=>6, 'name'=>'Birthday', 'parent'=>0]
];

还有这个功能,

function path($element_id, $input, $ids = [])
{
    if(!$ids) // for performance, make this once and pass it around
    {   
        $ids = array_column($input, 'id'); // array containing only 'id's of $input
    }   

    $current_key = array_search($element_id, $ids); // search for $input variable's current key

    unset($ids[$current_key]); // unsetting used keys to make above array search faster next time

    $current_element = $input[$current_key]; // get current element as array from $input

    $names[] = $current_element['name']; // create an array containing current element

    if($current_element['parent'] != 0) // check if current element have parent
    {   
        $names[] = path($current_element['parent'], $input, $ids); // call this function, let it return string, append it to $names
    }   

    return implode(' ⟶ ', array_reverse($names)); // make final return, seprate by ⟶
}

阅读echo path(13, $input);会返回

Anniversary ⟶ New arrives ⟶ Discount

这是相同功能的缩小版

function path($a,$b,$c=[]){if(!$c){$c=array_column($b,'id');}$d=array_search($a,$c);unset($c[$d]);$e=$b[$d];$f[]=$e['name'];if($e['parent']!=0){$f[]=path($e['parent'],$b,$c);}return implode(' ⟶ ',array_reverse($f));}

感谢代码审阅者 LoufyloufQuill

【讨论】:

    【解决方案3】:
    $find = 13;
    $path = array();
    
    function FindById ($arr, $find) {
      $k = null;
    
      foreach($arr as $key => $item) 
        if ($item['id'] == $find) 
          { $k = $key; break; }
      return $k;
    }
    
    if ( false === ($k = FindById($arr, $find)))  die("not found");
    
    while (true) {
       array_unshift($path, $arr[$k]['name']);
       if( !  $arr[$k]['parent']) break;
       if(false === ($k = FindById($arr, $arr[$k]['parent']))) die("illegal structure");
    
    }  
    
    echo implode('->', $path);
    

    【讨论】:

    • 有可能解决这个问题吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多