Получить путь из данных списка смежности

У меня есть массив (данные из таблицы смежности), и он выглядит так:

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
        )
)

И я ищу способ получить свой путь по идентификатору;

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;

Как я могу это сделать? Спасибо!


person XTRUST.ORG    schedule 25.06.2015    source источник
comment
И что вы пробовали   -  person Narendrasingh Sisodia    schedule 25.06.2015


Ответы (3)


Рассмотрим этот массив,

$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));}

Спасибо рецензентам кода Loufylouf и Перо

person viral    schedule 25.06.2015

person    schedule
comment
С какой целью вы пишете $key =>? Вы не используете его - person splash58; 25.06.2015
comment
Это бесполезно.. @splash58 - person Narendrasingh Sisodia; 25.06.2015
comment
@splash58 Я знаю, что работаю над этим, у тебя есть решение? - person Narendrasingh Sisodia; 25.06.2015
comment
$результат[$ключ] = $значение['имя']; затем эхо взорвется('-›',$результат); - person splash58; 25.06.2015
comment
Да, наконец.. Спасибо, приятель @splash58 - person Narendrasingh Sisodia; 26.06.2015

person    schedule
comment
Можно ли это исправить? - person XTRUST.ORG; 25.06.2015