1

I have an array of id's which are formed into a tree structure, for a storage type application.

Array image

The tree structure is then displayed similar to this

enter image description here

What I am looking to do is create a list of the full path of each array element.

[366 => 'Files',356 => 'Misc',354 => 'Photos',368 = 'Photos/Cities',375 = 'Photos/Cities/England',376 = 'Photos/Cities/Scotland']

The menu is user defined so this is just an example, it could have many more levels.

The name for each array element is added from an array of names ie names[376] (Photos)

I have tried several recursive functions and struggled, I'm hopeing someone who is much better at PHP than me can help! Thanks

10
  • 1
    not really a dupe, but very related, and maybe helpful: stackoverflow.com/questions/3544988/… Commented Mar 20, 2019 at 21:18
  • Did you mean: recursion Commented Mar 20, 2019 at 21:19
  • Where do the folder names come from, are they in the "array of ids"? Commented Mar 20, 2019 at 21:22
  • The folder names come from another array of names ie $names[376] would be Photos Commented Mar 20, 2019 at 21:27
  • I'm not clear on the data. How the parent relationship is expressed. Did you write the render code because I'm guessing understanding that fully you would be able to answer your own question. Code or data? Otherwise just gonna suggest generic web resources/tutorials. Commented Mar 20, 2019 at 21:30

2 Answers 2

1

I hope I understand what you want to achieve. In that case, this could be a solution

<?php

$ids = [
  354 => [
    368 => [
      375,
      376
    ]
  ],
  356,
  366
];

$names = [
    354 => "Photos", 
    368 => "Cities",
    375 => "England",
    376 => "Scotland",
    356 => "Files",
    366 => "Misc"
];


print_r(build_list($ids));

function build_list($ids, $path = ""){
  global $names;
  $list = [];
  foreach($ids as $key => $value){
    if(is_array($value)){
      //$list[$key] = $path . $names[$key]; // uncomment if you need output (2)
      $list = array_replace_recursive($list, build_list($value, ($path . $names[$key] . "/")))
    }else{
      $list[$value] = $path . $names[$value];
    }
  }
  return $list;
}

?>

Output (1)

Array
(
  [375] => Photos/Cities/England
  [376] => Photos/Cities/Scotland
  [356] => Files
  [366] => Misc
)

Output (2)

Array
(
  [354] => Photos
  [368] => Photos/Cities
  [375] => Photos/Cities/England
  [376] => Photos/Cities/Scotland
  [356] => Files
  [366] => Misc
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for this, it's kinda right apart from needing the array key to be the id of the last item in each array value...sorry not very clearArray ( [375] => Photos/Cities/England [376] => Photos/Cities/Scotland [356] => Files [366] => Misc )
@user3459394 Check the edited answer. A bit late but at least you have two solutions
1

This function will do what you want. It recursively traverses the tree, creating elements for each key and passing prefixes down through the recursion to create each element's name:

function make_paths($array, $names, $prefix = '') {
    $output = array();
    foreach ($array as $key => $arr) {
        $name = $prefix . ($prefix != '' ? '/' : '') . $names[$key];
        if (count($arr)) {
            $output = $output + make_paths($arr, $names, $name);
        }
        $output[$key] = $name;
    }
    return $output;
}

Output:

Array (
    [375] => Photos/Cities/England
    [376] => Photos/Cities/Scotland
    [368] => Photos/Cities
    [354] => Photos
    [356] => Misc
    [366] => Files 
)

Demo on 3v4l.org

1 Comment

I'm curious as to why you decided to unaccept this answer? It gives exactly the same results as the later one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.