I would like convert the below text into a nested array, something like you would get with MPTT database structure.
I am getting the data from a shell script and need to display it on a website. Don’t have any control over the format :/
There is lots of information about array -> list, but not much going the other way.
Any input would be appreciated, thanks.
cat, true cat
=> domestic cat, house cat, Felis domesticus, Felis catus
=> kitty, kitty-cat, puss
=> mouser
=> alley cat
=> tom, tomcat
=> gib
=> Angora, Angora cat
=> Siamese cat, Siamese
=> blue point Siamese
=> wildcat
=> sand cat
=> European wildcat, catamountain, Felis silvestris
=> cougar, puma, catamount, mountain lion, painter, panther, Felis concolor
=> ocelot, panther cat, Felis pardalis
=> manul, Pallas's cat, Felis manul
=> lynx, catamount
=> common lynx, Lynx lynx
=> Canada lynx, Lynx canadensis
You have got a sorted tree list here already. Each next line is either a child of the previous line or a sibling. So you can process over the list, get the name of an item, gain the level an item is in by it’s indentation and create an element out of it.
So every element has a name and zero or more children. From the input it can also be said which level it belongs to.
An element can be represented as an array, in which it’s first value is the name and the second value is an array for the children.
As the list is sorted, we can use a simple map, which per level is an alias to the children of a certain level. So with the level each element has, we can add it to the stack:
As this code-example shows, the stack/map for the current level is getting
$selfas children added:The stack for the level one higher, get’s the reference to the children of
$self(index1):So now per each line, we need to find the level. As this stack shows, the level is sequentially numbered:
0, 1, 2, ...but in the input it’s just a number of spaces.A little helper object can do the work to collect/group the number of characters in a string to levels, taking care that – if a level yet does not exist for an indentation – it is added, but only if higher.
This solves the problem that in your input there is no 1:1 relation between the size of the indentation and it’s index. At least not an obvious one.
This helper object is exemplary named
Levelsand implements__invoketo provide the level for an indent while transparently adding a new level if necessary:So now we can turn indentation into the level. That level allows us to run the stack. The stack allows to build the tree. Fine.
The line by line parsing can be easily done with a regular expression. As I’m lazy, I just use
preg_match_alland return – per line – the indentation and the name. Because I want to have more comfort, I wrap it into a function that does always return me an array, so I can use it in an iterator:Using on input with a pattern like
will give me an array per each line, that is:
You see the pattern here? It’s close to
With help of the
Levelsobject, this can be mapped, so just a call of a mapping function:From
array(line, indent, name)toarray(level, name). To have this accessible, this is returned by another function where theLevelscan be injected:So, everything is in order to read from all lines. However, this needs to be placed into the the tree. Remembering adding to the stack:
(
$elementis the name here). This actually needs the stack and the stack is actually the tree. So let’s create another function that returns this function and allow to push each line onto the stack to build the tree:So the last thing to do is just to process one element after the other:
So now with the
$inputgiven this creates an array, with only (root) child nodes on it’s first level and then having anarraywith two entries per each node:Name is a string here, children an array. So this has already done the list to array / tree here technically. But it’s rather burdensome, because you want to be able to output the tree structure as well. You can do so by doing recursive function calls, or by implementing a recursive iterator.
Let me give an Recursive Iterator Example:
This is just an array iterator (as all nodes are an array, as well as all child nodes) and for the current node, it returns the name. If asked for children, it checks if there are some and offers them again as a
TreeIterator. That makes using it simple, e.g. outputting as text:Output:
If you’re looking for more HTML output control in conjunction with an recursive iterator, please see the following question that has an example for
<ul><li>based HTML output:So how does this look like all together? The code to review at once as a gist on github.