T
T
TANK_IST2014-06-16 19:27:10
PHP
TANK_IST, 2014-06-16 19:27:10

How to write code for this task?

There are arrays:
Key - folder id, value - folder name :

Array(
 [3] => other
 [13] => MAZ
 [11] => LOL
 [9] => pack
)

Key - folder id, value - folder parent id :
Array(
 [13] => 11
 [11] => 9
)

Array of requirements :
Array(11, 13)
How to write code that composes folder paths from an array of requirements ?
For example:
For folder 11, the path would be: pack:LOL
For folder 13, the path would be: pack:LOL:MAZ

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2014-06-16
@TANK_IST

$dirMapping = [
 3 => 'other'
 13 => 'MAZ'
 11 => 'LOL'
 9 => 'pack'
];

$hierarchy = [
   13 => 11
   11 => 9
];

$ids = [11, 13];
$paths = [];
foreach ($ids as $current) {
    $path = [];
    // повторяем пока мы не дойдем до последнего
    while(isset($hierarchy[$current])){
        $path[] = $dirMapping[$current];
        $current = $hierarchy[$current];
    }; 
    $path[] = $dirMapping[$current];
    $paths[] = join(':', array_reverse($path));
}

Haven't tested it to work, but that's the gist of it. You can come up with a slightly better option but laziness.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question