PHP conversion of C# function to Fill Treeview

I have a C# function that takes a list of objects and fills a treeview with the data:

private void FillTree(IEnumerable<indexlistapp> data)
{
    Dictionary<int, TreeNode> parents = new Dictionary<int, TreeNode>();
    foreach (var a in data)
    {
        string price = "";
        int level = a.treelevel-1;
        if (a.price > 0)
        {
            price = string.Concat("PRICE:$", a.price.ToString(), " ");
        }

        if (level == 0 || parents.Count() == 0)
        {
            parents[level] = tvResults.Nodes.Add(String.Concat(a.app, a.lrflag, " ", price, "IC:", a.ic));
            parents[level].Tag = String.Concat(a.ic, "|", a.seq);
        }
        else
        {
            parents[level] =
                parents[level - 1].Nodes.Add(String.Concat(a.app, a.lrflag, " ", price, "IC:", a.ic));
            parents[level].Tag = String.Concat(a.ic, "|", a.seq);
        }
        parents[level].EnsureVisible();
    }

    if (tvResults.Nodes.Count > 0) tvResults.Nodes[0].EnsureVisible();
}

Here is what the data that is sent to the FillTree function looks like, converted to JSON, it is simply a List of indexlistapp objects (IEnumerable<indexlistapp> data):

[
    {"treelevel":"1","app":"Front","lrflag":null,"ic":null,"price":null,"parentlevel":"0","seq":"27"},
    {"treelevel":"2","app":"V-Series","lrflag":null,"ic":null,"price":null,"parentlevel":"1","seq":"28"},
    {"treelevel":"3","app":"opt J56","lrflag":null,"ic":null,"price":null,"parentlevel":"2","seq":"29"},
    {"treelevel":"4","app":"R.","lrflag":"R","ic":"536-01132AR","price":"693.00","parentlevel":"3","seq":"30"},
    {"treelevel":"4","app":"L.","lrflag":"L","ic":"536-01133AL","price":"693.00","parentlevel":"3","seq":"31"},
    {"treelevel":"3","app":"opt J63","lrflag":null,"ic":null,"price":null,"parentlevel":"2","seq":"32"},
    {"treelevel":"4","app":"R.","lrflag":"R","ic":"536-01130R","price":null,"parentlevel":"3","seq":"33"},
    {"treelevel":"4","app":"L.","lrflag":"L","ic":"536-01131L","price":null,"parentlevel":"3","seq":"34"},
    {"treelevel":"2","app":"exc. V-Series","lrflag":null,"ic":null,"price":null,"parentlevel":"1","seq":"35"},
    {"treelevel":"3","app":"opt JE5","lrflag":null,"ic":null,"price":null,"parentlevel":"2","seq":"36"},
    {"treelevel":"4","app":"AWD","lrflag":null,"ic":null,"price":null,"parentlevel":"3","seq":"37"},
    {"treelevel":"5","app":"R.","lrflag":null,"ic":"536-01142","price":null,"parentlevel":"4","seq":"38"},
    {"treelevel":"5","app":"L.","lrflag":null,"ic":"536-01143","price":null,"parentlevel":"4","seq":"39"},
    {"treelevel":"4","app":"RWD","lrflag":null,"ic":null,"price":null,"parentlevel":"3","seq":"40"},
    {"treelevel":"5","app":"R.","lrflag":null,"ic":"536-01143","price":null,"parentlevel":"4","seq":"41"},
    {"treelevel":"5","app":"L.","lrflag":null,"ic":"536-01142","price":null,"parentlevel":"4","seq":"42"},
    {"treelevel":"3","app":"opt J55","lrflag":null,"ic":null,"price":null,"parentlevel":"2","seq":"43"},
    {"treelevel":"4","app":"AWD","lrflag":null,"ic":null,"price":null,"parentlevel":"3","seq":"44"},
    {"treelevel":"5","app":"R.","lrflag":null,"ic":"536-01036","price":null,"parentlevel":"4","seq":"45"},
    {"treelevel":"5","app":"L.","lrflag":null,"ic":"536-01037","price":null,"parentlevel":"4","seq":"46"},
    {"treelevel":"4","app":"RWD","lrflag":null,"ic":null,"price":null,"parentlevel":"3","seq":"47"},
    {"treelevel":"5","app":"R.","lrflag":null,"ic":"536-01037","price":null,"parentlevel":"4","seq":"48"},
    {"treelevel":"5","app":"L.","lrflag":null,"ic":"536-01036","price":null,"parentlevel":"4","seq":"49"},
    {"treelevel":"1","app":"Rear","lrflag":null,"ic":null,"price":null,"parentlevel":"0","seq":"260"},
    {"treelevel":"2","app":"Base","lrflag":null,"ic":null,"price":null,"parentlevel":"1","seq":"261"},
    {"treelevel":"3","app":"opt JE5","lrflag":null,"ic":null,"price":null,"parentlevel":"2","seq":"262"},
    {"treelevel":"4","app":"R.","lrflag":"R","ic":"536-01038R","price":null,"parentlevel":"3","seq":"263"},
    {"treelevel":"4","app":"L.","lrflag":"L","ic":"536-01039L","price":null,"parentlevel":"3","seq":"264"},
    {"treelevel":"3","app":"opt J55","lrflag":null,"ic":null,"price":null,"parentlevel":"2","seq":"265"},
    {"treelevel":"4","app":"R.","lrflag":"R","ic":"536-01042R","price":null,"parentlevel":"3","seq":"266"},
    {"treelevel":"4","app":"L.","lrflag":"L","ic":"536-01043L","price":null,"parentlevel":"3","seq":"267"},
    {"treelevel":"2","app":"V-Series","lrflag":null,"ic":null,"price":null,"parentlevel":"1","seq":"268"},
    {"treelevel":"3","app":"R.","lrflag":"R","ic":"536-01134AR","price":"403.00","parentlevel":"2","seq":"269"},
    {"treelevel":"3","app":"L.","lrflag":"L","ic":"536-01135AL","price":"466.00","parentlevel":"2","seq":"270"}
]

I need to be able to build the JSON below in PHP with a function that takes the return from a PostgreSQL script:
This conversion doesn’t work, I guess I’m just not getting it:

function FillTree($d)     
{
    $parents = array();
    foreach ($d as $k=>$v)
    {
        foreach ($d[$k] as $key=>$value)
        {
            $price = "";
            $level = $d[$k][$key]-1;

            if (isset($d[$k]["price"]) && $d[$k]["price"] > 0)
            {
                $price = "PRICE:$". $d[$k]["price"]. " ";
            }

            if ($level == 0 || count($parents) == 0)
            {
                $parents[$level]["text"] = $d[$k]["app"] . $d[$k]["lrflag"] . " " . $d[$k]["price"] . "IC:" . $d[$k]["ic"];
                $parents[$level]["tag"] = $d[$k]["ic"];
            }
            else
            {
                $parents[$level-1]["text"] = $d[$k]["app"] . $d[$k]["lrflag"] . " " . $price . "IC:" . $d[$k]["ic"];
                $parents[$level-1]["tag"] = $d[$k][".ic"];
            }
        }    
    }
    return $parents;
}

Here is what I’m expecting:

[
    {
        "text": "Front IC:",
        "tags": [],
        "nodes": [
            {
                "text": "V-Series IC:",
                "tags": [],
                "nodes": [
                    {
                        "text": "opt J56 IC:",
                        "tags": [],
                        "nodes": [
                            {
                                "text": "R. R PRICE: $693 IC: 536-01132AR",
                                "tags": ["536-01132AR"]
                            },
                            {
                                "text": "L. L  PRICE: $693 IC: 536-01132AL",
                                "tags": ["536-01132AL"]
                            }
                        ]
                    },
                    {
                        "text": "opt J63",
                        "tags": [],
                        "nodes": [
                            {
                                "text": "R. R IC: 536-01130R",
                                "tags": ["536-01130R"]
                            },
                            {
                                "text": "L. L IC: 536-01131L",
                                "tags": ["536-01131L"]
                            }
                        ]
                    }
                ]
            },
            {
                "text":"exc. V-Series IC:",
                "tags": [],
                "nodes": [
                    {
                        "text": "opt JE5 IC:",
                        "tags": [],
                        "nodes": [
                            {
                                "text": "AWD IC:",
                                "tags": [],
                                "nodes": [
                                    {
                                        "text": "R. IC: 536-01142",
                                        "tags": ["536-01142"]
                                    },
                                    {
                                        "text": "L. IC: 536-01143",
                                        "tags": ["536-01143"]
                                    }
                                ]
                            },
                            {
                                "text": "RWD IC:",
                                "tags": [],
                                "nodes": [
                                    {
                                        "text": "R. IC: 536-01143",
                                        "tags": ["536-01143"]
                                    },
                                    {
                                        "text": "L. IC: 536-01142",
                                        "tags": ["536-01142"]
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "text": "opt J55 IC:",
                        "tags": [],
                        "nodes": [
                            {
                                "text": "AWD IC:",
                                "tags": [],
                                "nodes": [
                                    {
                                        "text": "R. IC: 536-01136",
                                        "tags": ["536-01136"]
                                    },
                                    {
                                        "text": "L. IC: 536-01137",
                                        "tags": ["536-01137"]
                                    }
                                ]
                            },
                            {
                                "text": "RWD IC:",
                                "tags": [],
                                "nodes": [
                                    {
                                        "text": "R. IC: 536-01137",
                                        "tags": ["536-01137"]
                                    },
                                    {
                                        "text": "L. IC: 536-01136",
                                        "tags": ["536-01136"]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    },
    {
        "text": "Rear IC:",
        "tags": [],
        "nodes": [
            {
                "text": "Base IC:",
                "tags": [],
                "nodes": [
                    {
                        "text": "opt JE5 IC:",
                        "tags": [],
                        "nodes": [
                            {
                                "text": "R. IC: 536-01038R",
                                "tags": ["536-01038R"]
                            },
                            {
                                "text": "L. IC: 536-01039L",
                                "tags": ["536-01039L"]
                            }
                        ]
                    },
                    {
                        "text": "opt J55 IC:",
                        "tags": [],
                        "nodes": [
                            {
                                "text": "R. IC: 536-01042R",
                                "tags": ["536-01042R"]
                            },
                            {
                                "text": "L. IC: 536-01043L",
                                "tags": []
                            }
                        ]
                    }
                ]
            },
            {
                "text": "V-Series IC:",
                "tags": [],
                "nodes": [
                    {
                        "text": "R. R PRICE: $403.00 IC: 536-01134AR",
                        "tags": ["536-01134AR"]
                    },
                    {
                        "text": "L. L PRICE: $466.00 IC: 536-01135AL",
                        "tags": ["536-01135AL"]
                    }
                ]
            }
        ]
    }
]

And what do you get, how does it differ from what you need?

Can you expand on that? Does it do nothing at all, give you error messages, how does it not work?

For me, this part looks a little strange, though I’m by no means an expert on this and it could be perfectly valid.

    foreach ($d as $k=>$v)
    {
        foreach ($d[$k] as $key=>$value)
        {
            $price = "";
            $level = $d[$k][$key]-1;

Inside the first foreach() loop, I’d have expected you to be referencing $k[] as your array, not referencing the “main” object. Same goes for the second loop, I’d expect you to use $key[], not still be using $d. But, it might be valid. It just seems to vary from your original code:

   foreach (var a in data)
    {
        string price = "";
        int level = a.treelevel-1;
        if (a.price > 0)
        {
            price = string.Concat("PRICE:$", a.price.ToString(), " ");
        }

I’m not seeing anywhere that you actually reference data here, which is the equivalent of what your PHP code is doing. You always access a, which is the variable that the foreach loop creates for each iteration.

I’m also not sure why you’ve got a second nested foreach() loop in the PHP code, where you don’t have that in the original code. To me, that first section might be more like

foreach ($d as $a) {
  $price = "";
  $level = $a["treelevel"] - 1;
  if ($a["price"] > 0) { 
    $price = "PRICE:$" . $a["price"];
    }
  ...

It shows 476 items when I’m expecting a lot less. I don’t want to post the whole thing here.

Well, in C#, the a in the for loop is the item in the List, essentially the current indexlistapp object in the list named data.

It is because in the first loop, the $d is actually an array, so you need to loop through those to get to the values.

Exactly - the same in PHP except that the parameters are reversed. So, rather than referencing $d[] as you do, you should reference the individual item, in my example above that would be $a.

I presumed this is basically an array of arrays, so you want to grab each “inner” array one at a time and build your output from that.

If I use your thought process about the loops like this:

foreach ($d as $a) 
{
    $price = "";
    $level = $a["treelevel"]-1;
    if (isset($a["price"]) && $a["price"] > 0)
    {
        $price = "PRICE:$". $a["price"]. " ";
    }
    if ($level == 0 || count($parents) == 0)
    {
        $parents[$level]["text"] = $a["app"] . $a["lrflag"] . " " . $a["price"] . "IC:" . $a["ic"];
        $parents[$level]["tag"] = $a["ic"];
    }
    else
    {
        $parents[$level-1]["text"] = $a["app"] . $a["lrflag"] . " " . $price . "IC:" . $a["ic"];
        $parents[$level-1]["tag"] = $a["ic"];
    }
}

Then I get this tree:

[
    {
        "text":"V-Series IC:",
        "tag":null
    },
    {
        "text":"L.L PRICE:$466.00 IC:536-01135AL",
        "tag":"536-01135AL"
    },
    {
        "text":"L.L IC:536-01043L",
        "tag":"536-01043L"
    },
    {
        "text":"L. IC:536-01036",
        "tag":"536-01036"
    }
]

Incomplete…

I’m not that familiar with C#, but what does this line do?

 parents[level] = tvResults.Nodes.Add(String.Concat(a.app, a.lrflag, " ", price, "IC:", a.ic));

Is it just the same as

 $parents[$level]["text"] = $a["app"] . $a["lrflag"] . " " . $a["price"] . "IC:" . $a["ic"];

I’m not sure where tvResults comes from in your original code. I’m also not sure how a C# Dictionary compares to a PHP array. Can you just transcribe one to the other?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.