0

I am doing MySQL query to get a listing files/directories. Each one has an id, and optionally and "parent_id" that relates each file or directory to it's containing directory. It's a flat array off files and directories though. How can I, using php, build this into a nested array something like this where each "Files" can have multiple child "Files" recursively:

[{"Files":{"id": "1","name":"Parent", "parent_id":"null" "Files":{"id": "2","name":"Child", "parent_id":"1"}}, {"Files":{"id": "3","name":"Another top level dir", "parent_id":"null"}]

Thanks!

Hippocrates
  • 2,500
  • 1
  • 18
  • 32

1 Answers1

2

What you need is a recursive function to assemble your nested array. Here are some links that should help you to get started.

Community
  • 1
  • 1
curtisdf
  • 4,042
  • 4
  • 29
  • 42