Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4032776
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:45:50+00:00 2026-05-20T11:45:50+00:00

I have this php code: $lines = file(data.csv); $nested = array(); $links = array();

  • 0

I have this php code:

$lines = file("data.csv");
$nested = array();
$links = array();

// first, create a structure that contains the connections between elements
foreach ($lines as $line) {
  list($child, $parent) = explode(",", $line);
  if (trim($child) == trim($parent)) {
    $nested[$parent] = null;
  } else {
    // add it to the children of parent
    $links[$parent][] = $child;
  }
}

function process(&$arr) {
  global $links;
  foreach ($arr as $key => $value) {
    // no more children => stop recursion
    if (!array_key_exists($key, $links)) {
      $array[$key] = null;
      continue;
    }
    // insert its children
    $arr[$key] = array_flip($links[$key]);
    // recurse down
    process($arr[$key]);
  }
}


function print_html($multi_dimensional_array)
{
    $m = $multi_dimensional_array;
    $keys = array();

    foreach($m as $key=>$value) {
            $keys[] = $key;
    }

    $i = 0;

    while($i < count($multi_dimensional_array)) {
        echo '<li><a href="#">'.$keys[$i].'</a>';
        if(is_array($multi_dimensional_array[$keys[$i]])) {
                echo '<ul>';
                print_html($multi_dimensional_array[$keys[$i]]);
                echo '</ul>';
        }
        echo '</li>';
        $i++;
    }
}

process($nested);

print_html($nested);

The data.csv format is (value,parent), an example would be:

one,one
two,two
three,three
sub_one,one
sub_one2,one
sub_two,two
sub_two2,two
sub_two3,two
sub_three,three
sub_sub_one,sub_one
sub_sub_one2,sub_one

Basically this what this PHP code does is create a multidimensional array that contains the parent name as key and the childs as values, and if a child also contains sub childs then it would be a key which contains subkeys, etc… and it will then print an html formatted list for that array.

How could I use C# to do what this php code does?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:45:51+00:00Added an answer on May 20, 2026 at 11:45 am

    Arrays in C# aren’t like arrays in PHP. You’ll probably need to implement the Composite pattern to get the desired data structure. That’s just a pointer to the right (I think) direction for a small part of your task. If you do end up using the pattern, consider Dictionary<string, TComposite> for the type of children collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my PHP code I have this: $filename = 'data.xml'; $xml = file_get_contents($filename); $data
I have the following code: <?php $FILE=giant-data-barf.txt; $fp = fopen($FILE,'r'); //read everything into data
I have this php code, with which I am trying to generate a popup
I have this PHP code echo '<a href=# onclick=updateByQuery(\'Layer3\', ' . json_encode($query) . ');>Link
Say i have this PHP code: $FooBar = a string; i then need a
I have this code <?php session_start(); if (isset($_GET[cmd])) $cmd = $_GET[cmd]; else die(You should
i need help with disk_total_space function.. i have this on my code <?php $sql=select
Let's say I have some code like this: <html> <head><title>Title</title></head> <body> <?php if (!$someCondition){
This is driving me crazy. I have this one php file on a test
I have noticed that cURL in PHP returns different data when told to output

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.