0

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
    )

)
Community
  • 1
  • 1
kostya
  • 209
  • 3
  • 11
  • HI Kostya. Your array values are like 'catalog.iden = catalog_sub.parent'? It's hard to understand exactly what data your code starts with and where it's trying to get. It might help if you post your MySQL table structure. – Buttle Butkus Aug 26 '13 at 04:08
  • I would advise you to include as little irrelevent information into your question as possible. Your array values are extremely weird. Can you replace them with something simpler, like `Dad`, `Grampa`, `Son`, or something that is less specific to your system? Your question is just not clear. – Buttle Butkus Aug 26 '13 at 07:09
  • Field name is any, table name is any, only relation has mean. – kostya Aug 26 '13 at 09:22

0 Answers0