I have read the Build a tree from a flat array in PHP and the Convert a series of parent-child relationships into a hierarchical tree? but could not figure out how to implement it for my problem. There is a pairs of relations as such:
Array
(
[0] => catalog.iden = catalog_sub.parent
[1] => catalog.group = catalog_group.iden
[2] => catalog_photo.iiden = catalog.iden
)
I have not NULL, or 0, or any explicit condition to stop looping. All what I know is what field has primary key with auto_increment attribute. Say in that example it will be field named 'iden' so in the relation it will be "parent".
I have spent a week to resolve this problem but with no result. So could anybody help me to arrange this array in order of relations. It may be tree of flat array.
Thanks in advance.
UPDATE
So far I only managed to do so:
foreach($matches[0] as &$match) {
$ref = explode('=', $match); // split by = sign
$lft = explode('.', $ref[0]);
$rgt = explode('.', $ref[1]);
if($lft[1] == 'iden') {
$arr[$lft[0]][$rgt[0]] = $rgt[1];
} else {
$arr[$rgt[0]][$lft[0]] = $lft[1];
}
}
What gives sorting by primary key and grouping by used tables:
Array
(
[catalog] => Array
(
[catalog_sub] => parent
[catalog_photo] => iiden
)
[catalog_group] => Array
(
[catalog] => group
)
)